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. sudo apt-get update 没有公钥,无法验证下列签名

    在更新系统源后,输入sudo apt-get update之后出现提示: W: GPG 错误:http://archive.ubuntukylin.com:10006 xenial InRelease ...

  2. List集合的遍历方法

    估计你永远都不会忘记这三个方法了...... public static void main(String[] args) { //超级for循环遍历方法 List<String> lis ...

  3. spring-web中的StringHttpMessageConverter简介

    spring的http请求内容转换,类似netty的handler转换.本文旨在通过分析StringHttpMessageConverter 来初步认识消息转换器HttpMessageConverte ...

  4. 启动Eclipse时,启不起来JVM terminated. Exit code=-1

    启动Eclipse时,启不起来JVM terminated. Exit code=-1 出现错误了,不知道什么原因原本好好的Eclipse,今天早上出问题了,启动不起来还抛出JVM terminate ...

  5. 使用Excel2007去反复功能时要注意的一个问题

    作者:iamlaosong Excel2007有个去反复功能(菜单:数据----删除反复项).非常实用,过去须要用VBA编程实现的功能,如今点击一下图标即可了.去反复通常是指定某列或者某几列.依据这指 ...

  6. python学习(十)赋值、表达式、if、while、for

    明天以搞定这几个应该不难 赋值.表达式.if.while.for 函数.作用域.参数.函数高级话题 迭代和解析一.二 还有我的<30天自制操作系统>没看 #!/usr/bin/python ...

  7. CString 成员函数用法大全(转)

    CString( );例:CString csStr; CString( const CString& stringSrc );例:CString csStr("ABCDEF中文12 ...

  8. windows 10 python 2.7和python3.6共存解决方法和pip安装

    一.首先去python官网将两个版本下载并安装: 然后进入windows的环境变量,检查下面4个变量: 1.C:\Python272.C:\Python27\Scripts3.D:\software\ ...

  9. MySQL集群搭建

    在集群中的所有主机上安装提供集群功能的软件包 官网上下载的  mysql-cluster-gpl-7.3.12-linux-glibc2.5-x86_64.tar.gz 规划了5台服务器,10.10. ...

  10. Unix高级环境编程—进程控制(一)

    一.函数fork #include<unistd.h> pid_t  fork(void)                                                 ...