字符串排列组合
bool BiggerFirst(string s1,string s2) { int size=s1.size(); for (int i = 0; i < size; i++) { if (s1[i]>s2[i]) { return true; } else if (s1[i]<s2[i]) { return false; } } return true; } vector<string> makeNew(string s,char c) { string temp; temp.reserve(s.size()+1); temp.resize(s.size()+1); vector<string> s1(s.size()+1,temp); for (int i = 0; i <=s.size(); i++) { s1[i][i]=c; if (i==0) { for (int j = 1; j <=s.size(); j++) { s1[i][j]=s[j-1]; } } else if (i==s.size()) { for (int j = 0; j < s.size(); j++) { s1[i][j]=s[j]; } } else { for (int j = 0; j <i; j++) { s1[i][j]=s[j]; } for (int j= i+1; j <=s.size(); j++) { s1[i][j]=s[j-1]; } } } return s1; } vector<string> getPermutation(string A) { vector<vector<string>> s; vector<string> a(1,string(1,A[0])); s.reserve(20); s.resize(20); s[0]=a; for (int i = 1; i < A.size(); i++) { for (auto&tag1:s[i-1]) { vector<string> temp; temp=makeNew(tag1,A[i]); for (auto& tag2:temp) { s[i].push_back(tag2); } } } vector<string> result=s[A.size()-1]; for (int i = 1; i < result.size()-1; i++) { int j=i-1; while (BiggerFirst(result[i],result[j])) { j--; } if (i>j+1) { string temp=result[i]; for (int Count = i; Count >j+1; Count--) { result[Count]=result[Count-1]; } result[j+1]=temp; } } return result; }
亲爱的听众朋友我是你的代班DJ