UVA - 10098 Generating Fast

来源:互联网 发布:中国民企军火出口知乎 编辑:程序博客网 时间:2024/06/13 07:10

题目大意:给出一个字符串,要求按字典序输出他的所有变换

解题思路:小白书上有讲到该算法

#include<cstring>#include<cstdio>char str[20];char temp[20];void print(int len , char *temp, int cur) {int i, j;if(cur == len) {for(int i = 0; i < cur; i++)printf("%c",temp[i]);printf("\n");}else  for( i = 0; i < len ; i++) {if(!i || str[i] != str[i-1]){int c1 = 0; int c2 = 0;for(int j = 0; j < len; j++)if(str[j] == str[i])c1++;for(int j = 0; j < cur; j++)if(temp[j] == str[i])c2++;if(c2 < c1) {temp[cur] = str[i];print(len,temp,cur+1);}}}}int main() {int test;scanf("%d", &test);getchar();while(test--) {scanf("%s", str);int len = strlen(str);char t;for(int i = 0; i < len; i++)for(int j = i+1; j< len;j++)if(str[i]>str[j]) {t = str[i];str[i] = str[j];str[j]= t;}print(len,temp,0);printf("\n");}return 0;}


0 0
原创粉丝点击