字串全排列
阿新 • • 發佈:2019-01-31
/* 方法1:依次從字串中取出一個字元作為最終排列的第一個字元,對剩餘字元組成的字串生成全排列, 最終結果為取出的字元和剩餘子串全排列的組合。 #include <iostream> #include <string> using namespace std; void permute1(string prefix, string str) { if(str.length() == 0) //到字尾str的長度為0也就是沒有後綴 輸出 cout << prefix << endl; else { for(int i = 0; i < str.length(); i++) //字首第一次是“”,之後劃分 直 permute1(prefix+str[i], str.substr(0,i)+str.substr(i+1,str.length())); } } void permute1(string s) { permute1("",s); } int main() { //method1, unable to remove duplicate permutations. cout << "method1" << endl; permute1("ABA"); } 優點:該方法易於理解,但無法移除重複的排列,如:s="ABA",會生成兩個“AAB”。 利用交換的思想,具體見例項,但該方法不如上述方法容易理解 比如ABC ABC -- A和A交換ABC --B和C交換 ACB -- A和B交換BAC --A和C交換 BCA -- A和C交換CBA --B和A交換 CAB */ //#include <string.h> #include <stdio.h> void swap(char* x, char* y) { char tmp; tmp = *x; *x = *y; *y = tmp; } /* Function to print permutations of string This function takes three parameters: 1. String 2. Starting index of the string 3. Ending index of the string. */ void permute(char *a, int i, int n) { int j; if (i == n) printf("%s\n", a); else for (j = i; j <= n; j++)//為避免生成重複排列,當不同位置的字元相同時不再交換 { if(a[i] == a[j] && j != i){ continue; } swap((a+i), (a+j)); permute(a, i+1, n); swap((a+i), (a+j)); //backtrack 為了下次另一種交換得換回去 } } int main() { //method2 //cout << "method2" << endl; char a[] = "ABA"; permute(a,0,2); return 0; } /* acm應用: 題目描述: 給定一個由不同的小寫字母組成的字串,輸出這個字串的所有全排列。 我們假設對於小寫字母有'a' < 'b' < ... < 'y' < 'z',而且給定的字串中的字母已經按照從小到大的順序排列。 輸入: 輸入只有一行,是一個由不同的小寫字母組成的字串,已知字串的長度在1到6之間。 輸出: 輸出這個字串的所有排列方式,每行一個排列。要求字母序比較小的排列在前面。字母序如下定義: 已知S = s1s2...sk , T = t1t2...tk,則S < T 等價於,存在p (1 <= p <= k),使得 s1 = t1, s2 = t2, ..., sp - 1 = tp - 1, sp < tp成立。 樣例輸入: abc 樣例輸出: abc acb bac bca cab cba 提示: 每組樣例輸出結束後要再輸出一個回車。 #include <stdio.h> #include <stdlib.h> #include <string.h> struct seq { char str[7]; }; struct seq seqs[721]; int count; int is_swap(char *str, int begin, int k) { int i, flag; for (i = begin, flag = 1; i < k; i ++) { if (str[i] == str[k]) { flag = 0; break; } } return flag; } void swap(char *str, int a, int b) { char temp; temp = str[a]; str[a] = str[b]; str[b] = temp; } void permutation_process(char *name, int begin, int end) { int k; if (begin == end - 1) { strcpy(seqs[count].str, name); count ++; }else { for (k = begin; k < end; k ++) { if (is_swap(name, begin, k)) { swap(name, k, begin); permutation_process(name, begin + 1, end); swap(name, k, begin); } } } } int compare(const void *p, const void *q) { const char *a = p; const char *b = q; return strcmp(a, b); } int main() { char name[7]; int i, len; while (scanf("%s", name) != EOF) { count = 0; len = strlen(name); permutation_process(name, 0, len); qsort(seqs, count, sizeof(seqs[0]), compare); for (i = 0; i < count; i ++) { printf("%s\n", seqs[i].str); } printf("\n"); } return 0; } */
算法系列操作全部參考自網上資料
比如:
http://blog.csdn.net/naruto2011sasuke/article/details/46634525
http://blog.csdn.net/han_xiaoyang/article/details/12163251#
http://blog.csdn.net/naruto2011sasuke/article/details/46634525
http://www.ahalei.com/forum-63-1.html等等