|
Rikka with stringTime 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, 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
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
Sample Output
|
题目大意:给一个字符串,“?”处用字母替换,要求字符串不回文且字典序最小。
代码如下:
#include <cstdio>
#include <cstring>
char a[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int main()
{
int i,j,n;
char s[1005];
while(scanf("%d",&n)!=EOF)
{
scanf("%s",s);
int ed,p=0,m=strlen(s);//p=1不回文,p=0回文
m/=2;
for(i=0;i<m;i++)//前一半字符串的转换
{
if(s[i]!='?'&&s[n-i-1]!='?'&&s[i]!=s[n-1-i])//对应位置字母不同一定不是回文
{
p=1;
continue;
}
if(!p && s[i]=='?')
{
if(s[n-i-1]=='?' && s[i]=='?') //前面满足回文条件时,...a....b...就是最小字典序且满足不回文条件
{
s[i]='a';
s[n-1-i]='b';
p=1;
}
else
{
for(j=0;j<26;j++) //前面填与后面不同的最小字母时,满足回文条件
if(a[j]!=s[n-1-i])
{
s[i]=a[j];
p=1;
break;
}
}
}
if(p&&s[i]=='?') //满足不回文条件后,后面所有'?'处都为a
s[i]='a';
}
if(n%2&&s[m]=='?') //如果时奇数长度,中间"?"一定改为a
s[m]='a';
if(n%2) //后一半字符串从后往前判断的结束位置
ed=m+1;
else
ed=m;
for(i=n-1;i>=ed;i--)
{
if(p&&s[i]=='?')
s[i]='a';
else if(!p && s[i]=='?')
{
for(j=0;j<26;j++)
if(a[j]!=s[n-1-i])
{
s[i]=a[j];
p=1;
break;
}
}
}
if(!p)
printf("QwQ\n");
else
printf("%s\n",s);
}
return 0;
}