bestcoder Rikka with string

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(1n1000) . 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
 
   
 
   
 
   
 
   
#include <iostream>
#include <string>
#include <string.h>
using namespace std;


int main()
{
    int n;
    while(cin>>n)
    {
        char str[1000];
        cin>>str;
        if(n==1)
        {
            cout<<"QwQ"<<endl;
            continue;
        }
        int len=strlen(str);
        int flag1=1,flag2=0,count=0;
        int A[1000],index[1000];
        for(int i=0;i<len;i++)
            if(str[i]=='?')
              {
                  index[count]=i;
                  A[count]=0;
                  count++;
              }
        if(count==0)
           flag1=0;
        if(flag1==1)
        {
            int cnt=0;
            while(cnt<=count)
            {
                for(int i=0; i<count; i++)
                    str[index[i]]=char(A[i]+'a');
                for(int i=0; i<=(len-1)/2; i++)
                    if(str[i]!=str[len-1-i])
                    {
                        cout<<str<<endl;
                        flag2=1;
                        break;
                    }
                if(flag2==1)
                    break;
                int sum=1;
                for(int i=count-1; i>=0; i--)
                {
                    sum=sum+A[i];
                    A[i]=sum%26;
                    sum=sum/26;
                    if(sum==0)
                        break;
                }
                for(int i=0; i<count; i++)
                    if(A[i]==25)
                        cnt++;
            }
            if(flag2==0)
                 cout<<"QwQ"<<endl;
        }
        else
        {
            int flag3=0;
            for(int i=0; i<=(len-1)/2; i++)
                if(str[i]!=str[len-1-i])
                {
                    cout<<str<<endl;
                    flag3=1;
                    break;
                }
           if(flag3==0)
              cout<<"QwQ"<<endl;
        }


    }
    return 0;
}

你可能感兴趣的:(bestcoder Rikka with string)