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

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#define LL long long
using namespace std;

char str[1005];
int len;
int flag;

int judge() {
    for(int i = 0; i <= len / 2; i++) {
        if(str[i] != str[len - i - 1]) return 1;
    }
    return 0;
}

void dfs(int x) {
    if(flag == 0) return;
    if(x == len) {
        if(judge()) {
            printf("%s\n", str);
            flag = 0;
        }
        return;
    }
    if(str[x] <= 'z' && str[x] >= 'a') {
        dfs(x + 1);
        return;
    }

    if(str[x] == '?') {
        for(int i = 'a'; i <= 'z'; i++) {
            str[x] = (char)i;
            dfs(x + 1);
            str[x] = '?';
        }
    }
}

int main() {
    int n;
    while(scanf("%d", &n) != EOF) {
        scanf("%s", str);
        len = strlen(str);
        flag = 1;
        dfs(0);
        if(flag) {
            printf("QwQ\n");
        }
    }
    return 0;
}
时间: 2024-08-06 15:58:13

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

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)

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

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])

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(dfs)bestcoder #53 div2 1002

题意: 输入一棵树,判断这棵树在以节点1为根节点时,是否是一棵特殊的树. 相关定义: 1.  定义f[A, i]为树A上节点i到节点1的距离,父节点与子节点之间的距离为1. 2.  对于树A与树B,如果A与B的节点数相同,且无论i为何值,f[A, i]与f[B, i]都相等,则A与B为两棵相似的树. 3.  对于一棵树A,在以节点1为根节点的情况下,如果不存在与其它树与A相似,则A是一棵特殊的树. 输入: 包含多组输入样例. 每组输入样例中首先输入一个整数n,表示一棵含有n个节点的树. 接下来n

hdu 1016 Prime Ring Problem (dfs)

一切见注释. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; bool vis[22]; int n; int ans[22]; int top; bool isprime(int x)//判断素数 { for(int i=2;i<x;i++) if(x%i==0)return false; return

hdu 3336 Count the string(KMP)

一道应用kmp算法中next数组的题目 这其中vis[i]从1加到n vis[i]=[next[i]]+1; #include<string.h> #include<stdlib.h> #include<stdio.h> #include<iostream> #include<algorithm> using namespace std; char s[200005]; int b; int next[200005]; int vis[20000

HDU 2209 翻纸牌游戏(dfs)

翻纸牌游戏 Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2180    Accepted Submission(s): 787 Problem Description 有一种纸牌游戏,很有意思,给你N张纸牌,一字排开,纸牌有正反两面,开始的纸牌可能是一种乱的状态(有些朝正,有些朝反),现在你需要整理这些纸牌.但是麻烦的是,每当你翻一张

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