Rikka with string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1033 Accepted Submission(s): 362
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?
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
Source
BestCoder Round #37 ($)
题目大意:一个只包含小写字母和问号的字符串,问号代表当前位的字符不确定,给出能够猜出的字典序最小的字符串
题目解法:
这道题真的很水,其实就是先把整个字符串的每一位问号都变成a,然后判断是否为回文,直到找到一位变成b能够导致当前串变成非回文串为止
题目大意:一个只包含小写字母和问号的字符串,问号代表当前位的字符不确定,给出能够猜出的字典序最小的字符串
题目解法:
这道题真的很水,其实就是先把整个字符串的每一位问号都变成a,然后判断是否为回文,直到找到一位变成b能够导致当前串变成非回文串为止
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#define MAX 1007
using namespace std;
int n;
char s[MAX];
bool mark[MAX];
bool check ( char s[] )
{
for ( int i = 0 ; i <= n/2+1 ; i++ )
if ( s[i] != s[n-1-i] ) return false;
return true;
}
int main ( )
{
while ( ~scanf ( "%d" , &n ) )
{
scanf ( "%s" , s );
int i = 0;
memset ( mark , 0 , sizeof ( mark ) );
while ( i < n )
{
if ( s[i] == '?' )
{
s[i] = 'a';
mark[i] = 1;
}
i++;
}
i = n-1;
while ( check ( s ) && i >= 0 )
{
if ( n&1 && i == n/2 )
{
i--;
continue;
}
if ( !mark[i] )
{
i--;
continue;
}
if ( s[i] == s[n-1-i] ) s[i] = 'b';
i--;
}
if ( check(s) ) puts ("QwQ");
else puts ( s );
}
}