注意可能有重复序列,所以用set先存储下来,最后输出时set会自动去重。。。
/**************************************** * author:crazy_石头 * algorithm:dfs * date:2013/10/17 * pro:UVA 10098 *****************************************/ #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <cstring> #include <climits> #include <algorithm> #include <iostream> #include <string> #include <set> using namespace std; set<string> myset; set<string>::iterator st; string ans; char str[12]; char temp[12]; bool used[12]; int len; inline void dfs(int cur) { if(cur==len) { ans=temp; myset.insert(ans); return ; } else { for(int i=0;i<len;i++) { if(!used[i]) { used[i]=1; temp[cur]=str[i]; dfs(cur+1); used[i]=0; } } } } int main() { int test; scanf("%d",&test); while(test--) { scanf("%s",str); myset.clear(); len=strlen(str); sort(str,str+len); memset(used,0,sizeof(used)); memset(temp,0,sizeof(temp)); dfs(0); for(st=myset.begin();st!=myset.end();st++) cout<<*st<<endl; cout<<endl; } return 0; } |