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. Windows 命令集合

    查看端口占用 查看8080端口使用情况: C:\>netstat -aon|findstr "8080" 结果:TCP    0.0.0.0:8080           0 ...

  2. VS中单元测试用法

    using System; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace UnitTestProject1 { [Test ...

  3. 卡特兰数-Catalan数

    卡特兰数的含义: 说到卡特兰数,就不得不提及卡特兰数序列.卡特兰数序列是一个整数序列.其通项公式是我们从中取出的就叫做第n个卡特兰数数,前几个卡特兰数数是:1, 1, 2, 5, 14, 42, 13 ...

  4. idea 的IDE

    idea 是与eclipse齐名的IDE(集成开发工具),以智能闻名,不过对于熟悉eclipse的的用户来说,初次接触idea有些让人搞不清方向,下面介绍一下简单的使用 方式. 1.安装 官网下载ul ...

  5. 【selenium+Python unittest】之使用smtplib发送邮件错误:smtplib.SMTPDataError:(554)、smtplib.SMTPAuthenticationError(例:126邮箱)

    原代码如下: import smtplib from email.mime.text import MIMEText from email.header import Header #要发送的服务器 ...

  6. 【转】【Axure学习】之短信动态验证码+图片动态验证码

    感谢:努力拼搏的80后的<巧用Axure三步轻松搞定图片验证码>. 人人都是产品经理的<Axure 教程:实现倒计时获取验证码效果>

  7. Codeforces Round #316 (Div. 2) (ABC题)

    A - Elections 题意: 每一场城市选举的结果,第一关键字是票数(降序),第二关键字是序号(升序),第一位获得胜利. 最后的选举结果,第一关键字是获胜城市数(降序),第二关键字是序号(升序) ...

  8. 【BZOJ2666】[cqoi2012]组装 贪心

    [BZOJ2666][cqoi2012]组装 Description 数轴上有m个生产车间可以生产零件.一共有n种零件,编号为1~n.第i个车间的坐标为xi,生产第pi种零件(1<=pi< ...

  9. EasyNVR无插件播放HLS/RTMP网页直播方案前端完善:监听表单变动

    在上一篇博客中我们表述完了防止提交成功后多余操作提交的一个过程:其中的精髓在于ajax的触发事件的使用. 而这篇博客主要想说明一下如何实时的判断出表单是否发生变化. 问题表述: 在网页前端的开发过程中 ...

  10. 九度OJ 1166:迭代求立方根 (迭代)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3695 解决:1700 题目描述: 立方根的逼近迭代方程是 y(n+1) = y(n)*2/3 + x/(3*y(n)*y(n)),其中y0 ...