hdu 5202(DFS)

Rikka with string

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1411    Accepted Submission(s): 502

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

暴力:直接枚举?处的答案,得到的第一个必定字典序最小

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
typedef long long LL;
char str[2005],res[2005];
int n;
bool flag;
bool judge(char *str){
    int len = strlen(str);
    for(int i=0,j=len-1;i<=j;i++,j--){
        if(str[i]!=str[j]) return true;
    }
    return false;
}
void dfs(int step){
    if(flag) return;
    if(step==n) {
        if(judge(str)){
            flag = true;
            strcpy(res,str);
        }
        return;
    }
    if(str[step]==‘?‘){
        for(int i=‘a‘;i<=‘z‘;i++){
            str[step]=i;
            dfs(step+1);
            str[step]=‘?‘;
        }
    }else{
        dfs(step+1);
    }
    return;
}
int main(){

    while(scanf("%d",&n)!=EOF){
        flag= false;
        scanf("%s",str);
        dfs(0);
        if(flag) printf("%s\n",res);
        else printf("QwQ\n");
    }
}
时间: 2024-10-10 10:01:03

hdu 5202(DFS)的相关文章

hdu 4109 dfs+剪枝优化

求最久时间即在无环有向图里求最远路径 dfs+剪枝优化 从0节点(自己增加的)出发,0到1~n个节点之间的距离为1,mt[i]表示从0点到第i个节点目前所得的最长路径 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<vector> using namespace std; const

HDU 1015 dfs回溯

题目真长.....看了好长时间才看懂.. 就是给你一个32位数字和一个最多15个字符的字符串,从字符串中选出5个字符,若满足题中给的那个式子,输出字典序最大的那5个字符,若不满足,输出no solution. 为了解决字典序问题,在输入字符串后,把字符串按从大到小排一下序,搜索一下若满足条件输出即可. 贴代码. 1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 #include <

hdu 1312 DFS算法简单题

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 此题与油田那题很像是练习深搜的好题,题意是从"@"开始,遍历整个图,找到连接的 "."有多少个 但要考虑变化,简单处理下就行了,主要是斜角的不要了,而且判断结束方式也不一样 具体看代码吧 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

HDU 5143 DFS

分别给出1,2,3,4   a, b, c,d个 问能否组成数个长度不小于3的等差数列. 首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的 然后直接DFS就行了,但是还是要注意先判合法能否进入下层递归来减少内存消耗. /** @Date : 2017-09-27 15:08:23 * @FileName: HDU 5143 DFS.cpp * @Platform: Windows * @Author : Lweleth ([email protected]) * @Link :

HDU 1045 DFS暴搜

Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6229    Accepted Submission(s): 3506 Problem Description Suppose that we have a square city with straight streets. A map of a city is a s

hdu 2212 DFS(水题)

DFS Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4923    Accepted Submission(s): 3029 Problem Description A DFS(digital factorial sum) number is found by summing the factorial of every digit

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