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
 
题目意思就是在?处填入小写字母,要求输出字典序最小的非回文字符串,否则输出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;
}

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

  1. ACM学习历程—SNNUOJ1215 矩阵2(二分 && dfs)

    http://219.244.176.199/JudgeOnline/problem.php?id=1215 这是这次微软和百度实习面试的一道题,题目大意就是:有一个n*m的矩阵,已知它每一行都是不严 ...

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

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...

  3. ACM学习历程—HDU5422 Rikka with Graph(贪心)

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...

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

    Description Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数.   Input 每组数据占一行,代表四张卡片上的数字( ...

  5. 【HDU5421】Victor and String(回文树)

    [HDU5421]Victor and String(回文树) 题面 Vjudge 大意: 你需要支持以下操作: 动态在前端插入一个字符 动态在后端插入一个字符 回答当前本质不同的回文串个数 回答当前 ...

  6. The Preliminary Contest for ICPC Asia Xuzhou 2019 G Colorful String(回文自动机+dfs)

    这题建立一棵回文树,然后用dfs搜索答案,但是有一点需要注意,就是打vis的标记时,如果标记为1,那么在好几个节点都对同一个字符i打过标记,此时的搜索从字符i点回溯,回到它的父亲节点,搜索其它的字符, ...

  7. HDU 5421 Victor and String(回文树)

    Victor and String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/262144 K (Java/Othe ...

  8. HDU-6599 I Love Palindrome String(回文自动机+字符串hash)

    题目链接 题意:给定一个字符串\(|S|\le 3\times 10^5\) 对于每个 \(i\in [1,|S|]\) 求有多少子串\(s_ls_{l+1}\cdots s_r\)满足下面条件 \( ...

  9. 2019 Multi-University Training Contest 2 I.I Love Palindrome String(回文自动机+字符串hash)

    Problem Description You are given a string S=s1s2..s|S| containing only lowercase English letters. F ...

随机推荐

  1. vim 处理换行符

    1. 设置文件格式 :set fileformats=unix,dos 2. 查询当前文件格式 :set fileformat? 3. 转换文件格式 :set fileformat=dos 4. 设置 ...

  2. Shell脚本笔记 1

    函数别名 设置别名 alias name="command" alias ll="ls -laS" 取消别名 unalias name 求取数学表达式 valu ...

  3. spring源码解析之IOC容器(一)

    学习优秀框架的源码,是提升个人技术水平必不可少的一个环节.如果只是停留在知道怎么用,但是不懂其中的来龙去脉,在技术的道路上注定走不长远.最近,学习了一段时间的spring源码,现在整理出来,以便日后温 ...

  4. eclipse--windowBuilder

    https://www.eclipse.org/windowbuilder/ https://www.eclipse.org/windowbuilder/download.php Documentat ...

  5. python学习(二)python中的核心数据类型

    数据类型是编程语言中的很重要的一个组成部分,我所知道的有数据类型的好处有:在内存中存放的格式知道,规定了有哪几种可用的操作. 我的埋点:为什么要有数据类型 那么python中的数据类型有哪几种呢? 对 ...

  6. PHP合并数组+与array_merge的区别

    http://www.phpernote.com/php-string/351.html PHP中合并两个数组可以使用+或者array_merge,但这两个还是有区别的   主要区别是当两个或者多个数 ...

  7. 详解spring boot实现多数据源代码实战

    之前在介绍使用JdbcTemplate和Spring-data-jpa时,都使用了单数据源.在单数据源的情况下,Spring Boot的配置非常简单,只需要在application.propertie ...

  8. leetCode 90.Subsets II(子集II) 解题思路和方法

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  9. authority分层

     

  10. Times[2017-01-25at JiNan]

    Times[问题描述 ]小 y 作为一名资深的 dotaer,对视野的控制有着深刻的研究.每个单位在一段特定的时间内会出现在小 y 的视野内,除此之外的时间都在小 y 看不到的地方.在小 y 看来,视 ...