zoukankan      html  css  js  c++  java
  • 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 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
     
    Source
     
    题目意思就是在?处填入小写字母,要求输出字典序最小的非回文字符串,否则输出QwQ。
    这题用dfs对?处进行搜索就行。不过好久没写搜索,当时忘记回溯了,一直报WA。
     
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <set>
    #include <map>
    #include <vector>
    #include <queue>
    #include <string>
    #define inf 0xfffffff
    #define eps 1e-10
    
    using namespace std;
    
    int n;
    char str[1005];
    bool flag;
    
    void Input()
    {
        flag = false;
        getchar();
        for (int i = 0; i <= n; ++i)
        {
            str[i] = getchar();
        }
    }
    
    void Output()
    {
        int i = 0;
        while (str[i] != '
    ')
        {
            printf("%c", str[i]);
            i++;
        }
        printf("
    ");
    }
    
    bool Cheak()
    {
        int len = n/2;
        for (int i = 0; i < len; ++i)
        {
            if (str[i] != str[n-1-i])
                return false;
        }
        return true;
    }
    
    void dfs(int i)
    {
        if (flag)
            return;
        while (str[i] != '?' && str[i] != '
    ')
            i++;
        if (str[i] == '
    ')
        {
            if (!Cheak())
            {
                flag = true;
            }
            return;
        }
        if (str[i] == '?')
        {
            for (char j = 'a'; j <= 'z'; ++j)
            {
                str[i] = j;
                dfs(i+1);
                if (flag)
                    return;
                str[i] = '?';
            }
        }
    }
    
    int main()
    {
        //freopen("test.txt", "r", stdin);
        while (scanf("%d", &n) != EOF)
        {
            Input();
            dfs(0);
            if (flag)
                Output();
            else
                printf("QwQ
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    e621. Activating a Keystroke When Any Child Component Has Focus
    e587. Filling Basic Shapes
    e591. Drawing Simple Text
    e595. Drawing an Image
    e586. Drawing Simple Shapes
    e636. Listening to All Key Events Before Delivery to Focused Component
    在 PL/SQL 块的哪部分可以对初始变量赋予新值? (选择1项)
    Oracle数据库中,在SQL语句中连接字符串的方法是哪个?(选择1项)
    你判断下面语句,有什么作用?(单选)
    Oracle数据库表空间与数据文件的关系描述正确的是( )
  • 原文地址:https://www.cnblogs.com/andyqsmart/p/4430413.html
Copyright © 2011-2022 走看看