Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
自己写的递归,应该还能对大量重复优化,控制递归深度,但是不想搞了#include<stdio.h> struct NODE { char c; int n; }data[50]; char way[300]; int length=0,k=0; void dfs(int n) { int i=0; if(n==k-1) { for(i=0;i<length;i++) { if(data[i].n) { break; } } way[n]=data[i].c; for(i=0;i<k;i++) { printf("%c",way[i]); } printf("\n"); return; } for(i=0;i<length;i++) { if(data[i].n) { way[n]=data[i].c; data[i].n--; dfs(n+1); data[i].n++; } } return; } int main() { char temp[200]={0}; int i=0,n=0,t[50]={0}; scanf("%s",&temp); for(i=0;temp[i];i++) { t[temp[i]-'a']++; k++; } for(i=0;i<30;i++) { if(t[i]) { data[length].c=i+'a'; data[length++].n=t[i]; } } dfs(0); } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator