标签:
dfs:
1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 int n,t,a[1005],flag,v[1005]; 5 char s[1005]; 6 int f() 7 { 8 int i; 9 for (i=0;i<n/2;i++) 10 if (s[i]!=s[n-i-1]) return 0; 11 return 1; 12 } 13 void dfs(int p) 14 { 15 if (flag) return ; 16 if (p==t) 17 { 18 if (!f()) 19 { 20 flag=1; 21 printf("%s\n",s); 22 } 23 return ; 24 } 25 int i; 26 for (i=‘a‘;i<=‘z‘;i++) 27 { 28 if (!v[p]) 29 { 30 v[p]=1; 31 s[a[p]]=i; 32 dfs(p+1); 33 v[p]=0; 34 } 35 } 36 } 37 int main() 38 { 39 int i; 40 while (~scanf("%d",&n)) 41 { 42 t=0; 43 scanf("%s",&s); 44 for (i=0;i<n;i++) 45 { 46 if (s[i]==‘?‘) 47 a[t++]=i; 48 } 49 memset(v,0,sizeof(v)); 50 flag=0; 51 dfs(0); 52 if (!flag) printf("QwQ\n"); 53 } 54 }
1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 char s[1005]; 5 int n; 6 int f() 7 { 8 int i; 9 for (i=0;i<n/2;i++) 10 if (s[i]!=s[n-i-1]) return 0; 11 return 1; 12 } 13 int main() 14 { 15 int i,a[1005],k; 16 while (~scanf("%d",&n)) 17 { 18 k=0; 19 scanf("%s",&s); 20 for (i=0;i<n;i++) 21 { 22 if (s[i]==‘?‘) 23 { 24 s[i]=‘a‘; 25 a[k++]=i; 26 } 27 } 28 if (n==0){printf("QwQ\n");continue;} 29 if (k==0&&f()) {printf("QwQ\n");continue;} 30 if (!f()) {printf("%s\n",s);continue;} 31 s[a[k-1]]=‘b‘; 32 if (!f()) {printf("%s\n",s);continue;} 33 if (k==1) {printf("QwQ\n");continue;} 34 s[a[k-1]]=‘a‘; 35 s[a[k-2]]=‘b‘; 36 printf("%s\n",s); 37 } 38 return 0; 39 }
hdu 5202 Rikka with string (dfs )
标签:
原文地址:http://www.cnblogs.com/pblr/p/4774374.html