zoukankan      html  css  js  c++  java
  • 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(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<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
    ",res);
            else printf("QwQ
    ");
        }
    }
  • 相关阅读:
    使用数组实现简单线性表功能
    解析.NET 许可证编译器 (Lc.exe) 的原理与源代码剖析
    Entity Framework with NOLOCK
    64位CentOS 6.0下搭建LAMP环境
    如何正确看待Linq的DistinctBy扩展和ForEach扩展
    jQuery最佳实践
    大话数据结构-树
    hdu2534-Score
    WKE——Webkit精简的纯C接口的浏览器
    WM_ERASEBKGND官方解释(翻译),以及Delphi里所有的使用情况(就是绘制窗口控件背景色,并阻止进一步传递消息)
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5766218.html
Copyright © 2011-2022 走看看