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

题目意思就是在?处填入小写字母,要求输出字典序最小的非回文字符串,否则输出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] != ‘\n‘)
    {
        printf("%c", str[i]);
        i++;
    }
    printf("\n");
}

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] != ‘\n‘)
        i++;
    if (str[i] == ‘\n‘)
    {
        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\n");
    }
    return 0;
}
时间: 2024-10-18 12:38:08

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

ACM学习历程—HDU5423 Rikka with Tree(搜索)

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

ACM学习历程—HDU1716 排列2(dfs &amp;&amp; set容器)

Description Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数. Input 每组数据占一行,代表四张卡片上的数字(0<=数字<=9),如果四张卡片都是0,则输入结束. Output 对每组卡片按从小到大的顺序输出所有能由这四张卡片组成的4位数,千位数字相同的在同一行,同一行中每个四位数间用空格分隔. 每组输出数据间空一行,最后一组数据后面没有空行. Sample Input 1 2 3 4 1 1 2 3 0 1

ACM学习历程—UESTC 1226 Huatuo&#39;s Medicine(数学)(2015CCPC L)

题目链接:http://acm.uestc.edu.cn/#/problem/show/1226 题目就是构造一个对称的串,除了中间的那个只有1个,其余的两边都是对称的两个,自然答案就是2*n-1. 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <algorithm> #

ACM学习历程—HDU 4726 Kia&#39;s Calculation( 贪心&amp;&amp;计数排序)

DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so careless and alway forget to carry a number when the sum of two digits exceeds 9. For example, when she calculates 4567+5789, she will get 9246, and for 12

ACM学习历程—HDU 5023 A Corrupt Mayor&#39;s Performance Art(广州赛区网赛)(线段树)

Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money. Becaus

【HDU5421】Victor and String(回文树)

[HDU5421]Victor and String(回文树) 题面 Vjudge 大意: 你需要支持以下操作: 动态在前端插入一个字符 动态在后端插入一个字符 回答当前本质不同的回文串个数 回答当前回文串个数 题解 回文树前端插入的操作,学一学感觉并不难? 额外维护一下一个前端插入的\(last\) 然后就和后端插入一模一样,前端插入时就是前端插入的\(last\) 唯一会产生的影响的就是当前字符差完后, 发现现在的\(last\)就是整个串,那么就要更新另外一端的\(last\)为当前这一端

ACM学习历程—HDU5269 ZYB loves Xor I(位运算 &amp;&amp; dfs &amp;&amp; 排序)(BestCoder Round #44 1002题)

Problem Description Memphis loves xor very musch.Now he gets an array A.The length of A is n.Now he wants to know the sum of all (lowbit(Ai xor Aj) (i,j∈[1,n]) We define that lowbit(x)=2^k,k is the smallest integer satisfied ((x and 2^k)>0)Specially,

ACM学习历程—Hihocoder 1291 Building in Sandbox(dfs &amp;&amp; 离线 &amp;&amp; 并查集)

http://hihocoder.com/problemset/problem/1291 前几天比较忙,这次来补一下微软笔试的最后一题,这题是这次微软笔试的第四题,过的人比较少,我当时在调试B题,没时间看这一题.不过打过之前一场BestCoder的应该都会有点思路,虽然BC那题是二维,这题是三维的,但是思路应该是一样的,没错,就是离线加并查集. 正过来考虑的时候,发现第一个要求相邻块是好处理的,但是第二个要求能否到达(1000, 1000, 1000)这个条件似乎比较难判断,当时BC上的题根据题

ACM学习历程—BestCoder Round #75

1001:King's Cake(数论) http://acm.hdu.edu.cn/showproblem.php?pid=5640 这题有点辗转相除的意思.基本没有什么坑点. 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <algorithm> #include &l