HDU 5202 Rikka with string (水DFS)

Rikka with string

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 624    Accepted Submission(s): 243

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(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 ($)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5202

题目大意:问号处填字母要求字典序最小且非回文

题目分析:遇到问号搜一下,字母从a到z枚举,搜完一组判断是否是回文,不是就退出,否则回溯继续搜

#include <cstdio>
#include <cstring>
char s[1005];
int n;
bool flag;

bool ok()
{
    for(int i = 0; i < n / 2; i++)
        if(s[i] != s[n - i - 1])
            return true;
    return false;
}

void DFS(int pos)
{
    if(pos == n)
    {
        if(ok())
            flag = true;
        return;
    }
    if(s[pos] != '?')
    {
        DFS(pos + 1);
        if(flag)
            return;
    }
    else
    {
        for(char j = 'a'; j <= 'z'; j++)
        {
            s[pos] = j;
            DFS(pos + 1);
            if(flag)
                return;
            s[pos] = '?';
        }
    }
    return;
}

int main()
{
    while(scanf("%d", &n) != EOF)
    {
        flag = false;
        scanf("%s", s);
        DFS(0);
        if(flag)
            printf("%s\n", s);
        else
            printf("QwQ\n");
    }
}
时间: 2024-08-27 03:53:00

HDU 5202 Rikka with string (水DFS)的相关文章

HDU - 5202 - Rikka with string (DFS)

Rikka with string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 214    Accepted Submission(s): 109 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation

hdu 5202 Rikka with string(模拟)

Rikka with string Time 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

hdu 5202 Rikka with string (dfs )

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

HDU 6086 Rikka with String

Rikka with String http://acm.hdu.edu.cn/showproblem.php?pid=6086 题意: 求一个长度为2L的,包含所给定的n的串,并且满足非对称. 分析: AC自动机+状压dp. 首先给这个n个串,建立AC自动机.然后去枚举长度为L的一个串,就可以知道另一半了. 如果给定的串完全存在于左边或者右边,那么直接往AC自动机加入这个串或者取反后的反串.如果是跨越中间,那么暴力的把所有的串,从中间切开,然后判断是否合法,加入到AC自动机上就行,如果长度枚举

hdu Rikka with string (dfs)

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; char a[1000+100]; int vis[1000+100]; int n; int cnt; int ok; bool huiwen() { int l=0; int r=strlen(a)-1; while(l<r) { if(a[l]!=a[r])

ACM学习历程——HDU5202 Rikka with string(dfs,回文字符串)

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

hdoj - 5202 Rikka with string (BestCoder Round #37 ($))

http://acm.hdu.edu.cn/showproblem.php?pid=5202 字符串处理的题,要细心. 给定一个只包含小写字母和问号的字符串,让我们还原出本来的字符串,把问号替换成任意字符,如果有多种可能输出字典序最小的,原字符串不能是回文串. 首先判断有没有非法字符,然后是否包含问号,如果没有包含则判断是否是回文串,满足则表示不能还原 . 否则把所有问号都替换成‘a',但是可能构成回文,然后继续替换,直到不是回文为止. 1 #include <iostream> 2 #inc

hdu 5423 Rikka with Tree(dfs)

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: For a tree T, let F(T,i) be the distance between vertice 1 and vertice i.(The length of

HDU 5423 Rikka with Tree(水题)

Rikka with Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 292    Accepted Submission(s): 149 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation,