1. 程式人生 > >C++筆試題 字串處理

C++筆試題 字串處理

字串處理

描述

定義字串的以下幾種操作:
• reverse(A)獲得A字串的逆序字串,例如reverse(“abc”) = “cba”
• shuffle(A)獲得A隨機重排的字串,例如shuffle(“dog”) ∈ {“dog”, “dgo”, “odg”, “ogd”, “gdo”, “god”}
• merge(A1, A2),合併A1和A2兩個字串,合併過程中只保證A1和A2本身字母的順序,例如merge(“qwe”, “asd”)的可能結果有很多, “qweasd”和“qwased”都是可能的取值。現在給定一個字串S,S ∈merge(reverse(A), shuffle(A))。求以字母表順序排序的A的最小值。

輸入描述

輸入一個字串S。

輸出描述

輸出一個字串,為A的最小取值。

樣例輸入 1

abcdefgabcdefg

樣例輸出 1

agfedcb

提示

reverse(“agfedcb”) = “bcdefga”
shuffle(“agfedcb”) = “abcdefg”
merge(“bcdefga”, “abcdefg”) = “abcdefgabcdefg”

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <iterator> #include <algorithm> // next_permutation() and sort() using namespace std; // #define UNIT_TEST class CambrianStringProcessing { private: int factorial(const int n); bool checkSequence(const string &s, const string &Str); public: string reverse
(const string& s); vector<string> shuffle(const string& s); vector<string> merge(const string& A1, const string& A2); bool merge(const string& A1, const string& A2, const string &S); string findMinimumStringA(const string &S); }; string CambrianStringProcessing::reverse(const string& s) { string ts; ts.assign(s.rbegin(), s.rend()); return ts; } vector<string> CambrianStringProcessing::shuffle(const string& s) { string ts = s; vector<string> shuffleVector; for (int i = 1; i <= factorial(s.size()); i++) { next_permutation(ts.begin(), ts.end()); shuffleVector.push_back(ts); } return shuffleVector; } int CambrianStringProcessing::factorial(const int n) { int f = n; for (int i = f - 1; i >= 1; i--) { f *= i; } return f; } bool CambrianStringProcessing::checkSequence(const string &s, const string &Str) { int pos, prevPos = 0; for (int i = 0; i < s.size(); i++) { pos = Str.find(s[i], prevPos); if (pos != string::npos) { prevPos = pos + 1; } else { return false; } } return true; } bool CambrianStringProcessing::merge(const string& A1, const string& A2, const string &S) { if (true == checkSequence(A1, S) && true == checkSequence(A2, S)) { return true; } return false; } vector<string> CambrianStringProcessing::merge(const string& A1, const string& A2) { vector<string> mergeVector; string newStr = A1 + A2; for (int i = 1; i <= factorial(newStr.size()); i++) { next_permutation(newStr.begin(), newStr.end()); if (true == checkSequence(A1, newStr) && true == checkSequence(A2, newStr)) { mergeVector.push_back(newStr); } } return mergeVector; } string CambrianStringProcessing::findMinimumStringA(const string &S) { set<char> chS; for (int i = 0; i < S.size(); i++) { chS.insert(S[i]); } string A; A.assign(chS.begin(), chS.end()); string Ar; // reverse(A) vector<string> strVs; // save all results of shuffle(A) vector<string> strAs; // save all A string int size = A.size(); // the length of string A for (int i = 1; i <= factorial(size); i++) { next_permutation(A.begin(), A.end()); Ar = reverse(A); strVs = shuffle(A); bool found = false; for (int i = 0; false == found && i < strVs.size(); i++) { if (true == merge(Ar, strVs[i], S)) { #ifdef UNIT_TEST cout << "reverse(" << A << ") = " << Ar << endl; cout << "shuffle(" << A << ") = " << strVs[i] << endl; cout << "merge(" << Ar << ", "; cout << strVs[i] << ") = " << S << endl << endl; #endif return A; } } } return A; } int main() { CambrianStringProcessing cms; #ifdef UNIT_TEST string A("abc"); cout << cms.reverse(A) << endl; A = "dog"; vector<string> strVs = cms.shuffle(A); for (unsigned int i = 0; i < strVs.size(); i++) { cout << strVs[i] << endl; } string A1 = "qwe"; string A2 = "asd"; vector<string> strVm = cms.merge(A1, A2); for (unsigned int i = 0; i < strVm.size(); i++) { cout << strVm[i] << endl; } #endif string S; cin >> S; cout << cms.findMinimumStringA(S) << endl; return 0; }