【Bestcoder

1001 - Rikka with string

Accepts: 395
Submissions: 2281
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
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(1≤n≤1000). 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

WA得智商都被拉低了 QAQ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int dfs(int step,bool &state)//1:to check
{
int res;
if (step<m-1) res=dfs(step+1,state);
int pos=n-vec[step]-1;
cout<<step<<' '<<str[vec[step]]<<' '<<str[pos]<<endl;
for(char ch='a';ch<='z';ch++)
if (state&&str[pos]==ch)
{
if (ch<'z') continue;
else return -1;//error
}
else
{
str[vec[step]]=ch;
if (state&&str[pos]!='?'&&ch!=str[pos]) state=0;
if (ch!=str[pos]) return 1;
}
return -1;
}

AC:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define CLEAR(a) memset((a),0,sizeof((a)))

using namespace std;

typedef long long LL;
const double pi = acos(-1.0);
const int maxn=2e3;
const int inf=99999999;
const float eps=1e-3;

vector<int> vec;
int n,m;
char s[maxn],str[maxn];

int dfs(int step,bool &state)//1:to check
{

int pos=n-vec[step]-1;
for(char ch='a';ch<='z';ch++)
{
str[vec[step]]=ch;
if (str[pos]!='?'&&ch!=str[pos]) {state=0;}
if (step<m-1) dfs(step+1,state);
if (state&&str[pos]!='?'&&ch!=str[pos]||!state) return 1;
}

return -1;
}

int main()
{

while(~scanf("%d",&n))
{
vec.clear();
scanf("%s",s);
memcpy(str,s,sizeof(s));
for(int i=0;i<n;i++)
if (s[i]=='?')
vec.push_back(i);
m=vec.size();
bool tmp=1;

if (m>1||m==1&&!(n%2&&vec[0]==n/2))
{
if (dfs(0,tmp)>=0) puts(str);
else puts("QwQ");
}
else
{
bool ans=1;
for(int i=1;i<=strlen(s)/2;i++)
if (s[i-1]!=s[strlen(s)-i])
{
ans=0;break;
}
if (m==1&&n%2&&vec[0]==n/2) str[vec[0]]='a';
puts(ans?"QwQ":str);
}

}
return 0;
}
Author

Semprathlon / Simfae Dean

Posted on

04/11/2015

Updated on

07/19/2023

Licensed under

Comments