hdoj - 5202 Rikka with string (BestCoder Round #37 ($))

http://acm.hdu.edu.cn/showproblem.php?pid=5202

字符串处理的题,要细心。

给定一个只包含小写字母和问号的字符串,让我们还原出本来的字符串,把问号替换成任意字符,如果有多种可能输出字典序最小的,原字符串不能是回文串。

首先判断有没有非法字符,然后是否包含问号,如果没有包含则判断是否是回文串,满足则表示不能还原 。

否则把所有问号都替换成‘a‘,但是可能构成回文,然后继续替换,直到不是回文为止。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <vector>
 5 #include <cstring>
 6 #include <string>
 7 #include <algorithm>
 8 #include <string>
 9 #include <set>
10 #include <functional>
11 #include <numeric>
12 #include <sstream>
13 #include <stack>
14 #include <map>
15 #include <queue>
16
17 #define CL(arr, val)    memset(arr, val, sizeof(arr))
18
19 #define ll long long
20 #define inf 0x7f7f7f7f
21 #define lc l,m,rt<<1
22 #define rc m + 1,r,rt<<1|1
23 #define pi acos(-1.0)
24
25 #define L(x)    (x) << 1
26 #define R(x)    (x) << 1 | 1
27 #define MID(l, r)   (l + r) >> 1
28 #define Min(x, y)   (x) < (y) ? (x) : (y)
29 #define Max(x, y)   (x) < (y) ? (y) : (x)
30 #define E(x)        (1 << (x))
31 #define iabs(x)     (x) < 0 ? -(x) : (x)
32 #define OUT(x)  printf("%I64d\n", x)
33 #define lowbit(x)   (x)&(-x)
34 #define Read()  freopen("a.txt", "r", stdin)
35 #define Write() freopen("dout.txt", "w", stdout);
36 #define N 100005
37 using namespace std;
38
39 bool palindrome(char *str)
40 {
41     int len=strlen(str);
42     for(int i=0;i<len/2;i++)
43     {
44         if(str[i]!=str[len-i-1]) return 0;
45     }
46     return 1;
47 }
48 char s[1010];
49 int f[1010];
50 int main()
51 {
52    //Read();
53     int n;
54     while(scanf("%d",&n)!=EOF)
55     {
56         getchar();
57         gets(s);
58         //printf("%s\n",s);
59         bool flag=0;
60         for(int i=0;i<strlen(s);i++)
61         {
62             if(!((s[i]>=‘a‘&&s[i]<=‘z‘)||s[i]==‘?‘))  {flag=1;break;}
63         }
64         //printf("%d\n",flag);
65         if(flag) {printf("QwQ\n");continue;}
66         for(int i=0;i<strlen(s);i++)
67         {
68             if(s[i]==‘?‘) {flag=1;break;}
69         }
70         if(!flag&&palindrome(s)) {printf("QwQ\n");continue;}
71         int j=0;
72         memset(f,0,sizeof(f));
73         for(int i=0;i<strlen(s);i++)
74         {
75             if(s[i]==‘?‘)
76             {
77                 s[i]=‘a‘;
78                 f[j++]=i;
79             }
80         }
81         j--;
82         flag=0;
83         if(palindrome(s))
84         {
85             s[f[j]]=‘b‘;   //从后往前替换  注意 a??aa
86             while(palindrome(s))
87             {
88                 if(j==0) {flag=1;break;}
89                 j--;
90                 s[f[j]]=‘b‘;
91                 s[f[j+1]]=‘a‘;
92             }
93         }
94         if(flag) printf("QwQ\n");
95         else
96         printf("%s\n",s);
97     }
98     return 0;
99 }
时间: 2024-10-12 21:10:09

hdoj - 5202 Rikka with string (BestCoder Round #37 ($))的相关文章

HDU 5202 Rikka with string (水DFS)

Rikka with string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 624    Accepted Submission(s): 243 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation

HDU - 5202 - Rikka with string (DFS)

Rikka with string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 214    Accepted Submission(s): 109 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation

hdu 5202 Rikka with string(模拟)

Rikka with string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 625    Accepted Submission(s): 244 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation

hdu 5203 Rikka with wood sticks(Bestcoder Round #37)

Rikka with wood sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 446    Accepted Submission(s): 130 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situ

hdu 5202 Rikka with string (dfs )

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

HDU 5432 Rikka with Tree (BestCoder Round #53 (div.2))

题目大意:给你一个树 判断这棵树是否是独特的 一颗树是独特的条件:不存在一颗和它本身不同但相似的树 两颗树相似条件:两颗树中点的数量相等且相对应的点的深度相同 如第2个样例 4 1 2 2 3 1 4 与 4 1 2 1 4 3 4 如图:这两棵树的点的数量相等且相应的点的深度deep相同,所以这两棵树相似,所以样例2存在一颗树与它不同但相似,即不特殊 运用广搜统计每个点的深度 要想一颗树特殊,只有保证他的分支下面不再有子节点 #include<stdio.h> #include<str

[BestCoder Round #3] hdu 4909 String (状压,计数)

String Problem Description You hava a non-empty string which consists of lowercase English letters and may contain at most one '?'. Let's choose non-empty substring G from S (it can be G = S). A substring of a string is a continuous subsequence of th

HDU-5284-wyh2000 and a string problem(BestCoder Round #48 ($))

wyh2000 and a string problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 492    Accepted Submission(s): 239 Problem Description Young theoretical computer scientist wyh2000 is teaching you

Rikka with string BC#37

Rikka with string Accepts: 395 Submissions: 2281 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的: 有一天勇太得到了一个长度为n的字符串,但是六花一不小心把这个字符串搞丢了.于是他们想要复原这一个字符串.勇太记得这个字符串只包含小写字母而且这