码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5202 Rikka with string (dfs )

时间:2015-08-31 23:27:30      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

Problem Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:
One day, Yuta got a string which contains n letters but Rikka lost it in accident. Now they want to recover the string. Yuta remembers that the string only contains lowercase letters and it is not a palindrome string. Unfortunately he cannot remember some letters. Can you help him recover the string?
It is too difficult for Rikka. Can you help her?
 

 

Input
This problem has multi test cases (no more than 20). For each test case, The first line contains a number n(1n1000). The next line contains an n-length string which only contains lowercase letters and ‘?’ – the place which Yuta is not sure.
 

 

Output
For each test cases print a n-length string – the string you come up with. In the case where more than one string exists, print the lexicographically first one. In the case where no such string exists, output “QwQ”.
 

 

Sample Input
5
a?bb?
3
aaa
 

 

Sample Output
aabba
QwQ

 

  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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!