码迷,mamicode.com
首页 > 其他好文 > 详细

Rikka with string

时间:2015-04-12 17:49:33      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:acm

 
 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(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 ?rst 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 <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>

using namespace std;
#define maxn 1000 + 10

int m[maxn], n;
string s;
bool flag;
int cnt;

bool judge(string str)
{
    int len = str.size();
    for(int i=0; i<len/2; i++)
    {
        if(s[i] != s[len-1-i])
            return false;
    }
    return true;
}

void dfs(int num)
{
    if(flag) return ;
    if(num == cnt)
    {
        if(!judge(s))
            {
                cout<<s<<endl;
                flag = true;
            }
          return ;
    }
    for(char i='a'; i<='z'; i++)
    {
        s[m[num]] = i;
        dfs(num + 1);
    }
    return ;
}

int main()
{
    while(~scanf("%d", &n))
    {
           flag = false;
           cnt = 0;
           cin>>s;
           memset(m, -1, sizeof(m));
           for(int i=0; i<n; i++)
           {
               if(s[i] == '?')
                m[cnt++] = i;
           }
           if(!cnt)
           {
               if(!judge(s))
                cout<<s<<endl;
               else cout<<"QwQ"<<endl;
           }
           else {
                dfs(0);
                if(!flag) cout<<"QwQ"<<endl;
           }

    }
}

Rikka with string

标签:acm

原文地址:http://blog.csdn.net/dojintian/article/details/45011571

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!