题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5202
注意这组数据就行了,其他的应该没有什么大问题。
????aaa
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
int n;
bool is_pal(string s)
{
for(int i = 0; i < n; i++)
if(s[i] != s[n-1-i])
return false;
return true;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
int i,j,flag = 0,pos1,pos2;
string s;
cin>>s;
for(i = n-1; i >= 0; i--)
if(s[i] == '?')
{
flag = 1;
s[i] = 'a';
pos1 = i;
break;
}
for(; i >= 0; i--)
if(s[i] == '?')
{
s[i] = 'a';
pos2 = i;
break;
}
for(; i >= 0; i--)
if(s[i] == '?')
{
s[i] = 'a';
}
if(flag && is_pal(s))
{
while(s[pos1]<'z' && is_pal(s))
s[pos1]++;
if(is_pal(s))
{
s[pos2] = 'b';
s[pos1] = 'a';
}
}
if(is_pal(s))
printf("QwQ\n");
else
cout<<s<<endl;
}
return 0;
}
本文提供了一道来自HDU在线评测系统的编程题(PID 5202)的解答思路及代码实现。该题要求判断字符串是否为回文,并通过替换问号来打破回文特性。文章包含了一个C++程序示例,演示了如何通过替换特定字符确保字符串不再是回文串。
491

被折叠的 条评论
为什么被折叠?



