ACM学习历程——HDU5202 Rikka with string(dfs,回文字符串)
2015-04-15 22:14
483 查看
[align=left]Problem Description[/align]
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?
[align=left]Input[/align]
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.
[align=left]Output[/align]
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”.
[align=left]Sample Input[/align]
5
a?bb?
3
aaa
[align=left]Sample Output[/align]
aabba
QwQ
[align=left]Source[/align]
BestCoder Round #37 ($)
题目意思就是在?处填入小写字母,要求输出字典序最小的非回文字符串,否则输出QwQ。
这题用dfs对?处进行搜索就行。不过好久没写搜索,当时忘记回溯了,一直报WA。
代码:
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?
[align=left]Input[/align]
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.
[align=left]Output[/align]
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”.
[align=left]Sample Input[/align]
5
a?bb?
3
aaa
[align=left]Sample Output[/align]
aabba
QwQ
[align=left]Source[/align]
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; }
相关文章推荐
- ACM学习历程—HDU5423 Rikka with Tree(搜索)
- ACM学习历程—HDU5422 Rikka with Graph(贪心)
- ACM学习历程9——string基本字符系列容器
- HDU5202 Rikka with string
- ACM学习历程—ZOJ 3861 Valid Pattern Lock(dfs)
- ACM学习历程—Hihocoder 1291 Building in Sandbox(dfs && 离线 && 并查集)
- ACM学习历程——POJ3468 A Simple Problem with Integers(线段树)
- ACM学习历程—UESTC 1219 Ba Gua Zhen(dfs && 独立回路 && xor高斯消元)
- ACM学习历程24——最大回文子串
- ACM学习历程—BZOJ 2115 Xor(dfs && 独立回路 && xor高斯消元)
- HDU-5202-Rikka with string(DFS + WrongAnswer)
- ACM学习历程—HDU5269 ZYB loves Xor I(位运算 && dfs && 排序)(BestCoder Round #44 1002题)
- ACM学习历程—NPU 2015年陕西省程序设计竞赛网络预赛(正式赛)A题 小女警的异世界之战(dfs && 分治)
- hdu Rikka with string (dfs)
- HDU 5202 Rikka with string (水DFS)
- ACM学习历程—SNNUOJ1215 矩阵2(二分 && dfs)
- ACM学习历程—HDU1716 排列2(dfs && set容器)
- ACM学习历程—HDU2476 String painter(动态规划)
- HDU 5202 Rikka with string
- HDU 6086 Rikka with String(2017 Multi-University Training Contest 3)