Rikka with string

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 581    Accepted Submission(s): 227

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

 #include<stdio.h>
#include<string.h>
int cnt = ;
char st[ + ] ;
char rst [ + ] ;
int n , vis[ + ]; void rev (char s[ + ])
{
char tmp[ + ] ;
int k = , i ;
for (i = strlen (st) - ; i >= ; i-- , k ++) {
tmp[k] = st[i] ;
}
tmp[k] = '\0' ;
strcpy (s , tmp) ;
} int main ()
{
// freopen ("a.txt" , "r" , stdin ) ;
while (~ scanf ("%d" , &n)) {
getchar () ;
gets (st) ;
cnt = ;
for (int i = ; st[i] != '\0' ;i ++) {
if (st[i] == '?') {
vis[cnt ++] = i ;
st[i] = 'a' ;
}
}
int i ;
rev (rst) ;
if (strcmp (st , rst)== ) {
for (i = cnt - ; i >= ; i--) {
st[vis[i]] = 'b' ;
rev (rst) ;
if (strcmp (rst , st)!= ) {
break ;
}
st[vis[i]] = 'a' ;
}
if (i == - ) puts ("QwQ") ;
else puts (st) ;
}
else puts (st) ;
}
return ;
}
如果没有不是回文串的限制可以把所有的?都变成a,这样一定是字典序最小的。而现在有了回文串的限制,我们可以从小到大枚举最靠后的不在字符串正中心的问号选什么,其他的问号依然换成a,显然如果有解,这样一定可以得到字典序最小的解。注意特判没有问号以及问号在正中心的情况。
时间复杂度O(n)

hdu.5202.Rikka with string(贪心)的更多相关文章

  1. HDU 6086 Rikka with String

    Rikka with String http://acm.hdu.edu.cn/showproblem.php?pid=6086 题意: 求一个长度为2L的,包含所给定的n的串,并且满足非对称. 分析 ...

  2. HDU 6086 Rikka with String AC自动机 + DP

    Rikka with String Problem Description As we know, Rikka is poor at math. Yuta is worrying about this ...

  3. hdoj - 5202 Rikka with string (BestCoder Round #37 ($))

    http://acm.hdu.edu.cn/showproblem.php?pid=5202 字符串处理的题,要细心. 给定一个只包含小写字母和问号的字符串,让我们还原出本来的字符串,把问号替换成任意 ...

  4. hdu 6086 -- Rikka with String(AC自动机 + 状压DP)

    题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...

  5. HDU 6086 Rikka with String ——(AC自动机 + DP)

    这是一个AC自动机+dp的问题,在中间的串的处理可以枚举中断点来插入自动机内来实现,具体参见代码. 在这题上不止为何一直MLE,一直找不到结果(lyf相同写法的代码消耗内存较少),还好考虑到这题节点应 ...

  6. HDU 5831 Rikka with Parenthesis II(六花与括号II)

    31 Rikka with Parenthesis II (六花与括号II) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

  7. HDU 6093 - Rikka with Number | 2017 Multi-University Training Contest 5

    JAVA+大数搞了一遍- - 不是很麻烦- - /* HDU 6093 - Rikka with Number [ 进制转换,康托展开,大数 ] | 2017 Multi-University Tra ...

  8. HDU 5842 Lweb and String(Lweb与字符串)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  9. 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence

    // 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence // 题意:三种操作,1增加值,2开根,3求和 // 思路:这题与HDU 4027 和HDU 5634 ...

随机推荐

  1. memset的使用

    今天写程序的时候用了memset这个函数,我知道他是关于清空指针的,设置为0.但我用的时候,没有注意到他是以字节为单位进行操作的,改了半天其他程序内容.要注意的是,memset是对字字进行操作,所以以 ...

  2. Chrome扩展开发之一——Chrome扩展的文件结构

    目录: 0.Chrome扩展开发(Gmail附件管理助手)系列之〇——概述 1.Chrome扩展开发之一——Chrome扩展的文件结构 2.Chrome扩展开发之二——Chrome扩展中脚本的运行机制 ...

  3. python的闭包与装饰器

    原文发表在我的博客主页,转载请注明出处 前言 如果把python当作脚本语言,每次就是写个几十行上百行来处理数据的话,装饰器也许不是很必要,但是如果要开发一个大型系统,装饰器是躲不开的,最开始体会ry ...

  4. IDL简介与corba入门案例

    IDL接口定义语言简介   IDL用中立语言的方式进行描述,能使软件组建(不同语言编写的)间相互通信. IDL提供了一个桥来连接不同的系统. Corba 上的服务用IDL描述,将被映射为某种程序设计语 ...

  5. 机器学习中的矩阵方法03:QR 分解

    1. QR 分解的形式 QR 分解是把矩阵分解成一个正交矩阵与一个上三角矩阵的积.QR 分解经常用来解线性最小二乘法问题.QR 分解也是特定特征值算法即QR算法的基础.用图可以将分解形象地表示成: 其 ...

  6. Moqui学习Day2

    用户 本地化  消息和日志门面 用户门面用于管理当前用户和访问,登陆,授权及登出的信息.用户信息包括区域设置,时区以及币种/ec.user.nowTimestamp设置日期. 消息门面用于追踪用户的消 ...

  7. 每天一个linux命令(39):iostat命令

    Linux系统中的 iostat 是I/O statistics(输入/输出统计)的缩写,iostat工具将对系统的磁盘操作活动进行监视.它的特点是汇报磁盘活动统计情况,同时也会 汇报出CPU使用情况 ...

  8. Git 常用命令详解(二)

    Git 是一个很强大的分布式版本管理工具,它不但适用于管理大型开源软件的源代码(如:linux kernel),管理私人的文档和源代码也有很多优势(如:wsi-lgame-pro) Git 的更多介绍 ...

  9. Mvc多级Views目录 asp.net mvc4 路由重写及 修改view 的寻找视图的规则

    一般我们在mvc开发过程中,都会碰到这样的问题.页面总是写在Views文件夹下,而且还只能一个Controller的页面只能写在相应的以 Controller名命名的文件夹下.如果我们写到别处呢?那么 ...

  10. poj 1006 中国剩余定理解同余方程

    其实画个图就明白了, 该问题就是求同余方程组的解: n+d≡p (mod 23) n+d≡e (mod 28) n+d≡i (mod 33) #include "iostream" ...