Codeforces 196 D. The Next Good String
2 seconds
256 megabytes
standard input
standard output
In problems on strings one often has to find a string with some particular properties. The problem authors were reluctant to waste time on thinking of a name for some string so they called it good.
A string is good if it doesn't have palindrome substrings longer than or equal to d.
You are given string s, consisting only of lowercase English letters. Find a good string t with
length |s|, consisting of lowercase English letters, which is lexicographically larger than s.
Of all such strings string t must be lexicographically minimum.
We will call a non-empty string s[a ... b] = sasa + 1... sb (1 ≤ a ≤ b ≤ |s|) a substring of
string s = s1s2... s|s|.
A non-empty string s = s1s2... sn is
called a palindrome if for all i from 1 to n the
following fulfills: si = sn - i + 1.
In other words, palindrome read the same in both directions.
String x = x1x2... x|x| is lexicographically
larger than string y = y1y2... y|y|,
if either |x| > |y| and x1 = y1, x2 = y2, ...
, x|y| = y|y|,
or there exists such number r (r < |x|, r < |y|),
that x1 = y1, x2 = y2, ...
, xr = yr and xr + 1 > yr + 1.
Characters in such strings are compared like their ASCII codes.
The first line contains integer d (1 ≤ d ≤ |s|).
The second line contains a non-empty string s, its length is no more than 4·105 characters.
The string consists of lowercase English letters.
Print the good string that lexicographically follows s, has the same length and consists of only lowercase English letters. If such string does not exist,
print "Impossible" (without the quotes).
3
aaaaaaa
aabbcaa
3
zzyzzzz
Impossible
4
abbabbbabbb
abbbcaaabab
搜索+hash。
长度为L的回文串一定包括长度为L-2的回文串。
推断回文的时候仅仅要推断d和d+1不是回文串就能够了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; typedef unsigned long long int ull; const int maxn=500100; char s[maxn],r[maxn];
int n,d;
ull p[maxn],hash[maxn],rhash[maxn]; bool ok(int ed,int d)
{
ed++;
int st=ed-d+1;
if(st<0) return true;
if((rhash[ed]-rhash[st-1]*p[d])*p[st-1]!=hash[ed]-hash[st-1])
return true;
return false;
} bool dfs(int x,int t)
{
if(x==n)
{
puts(r);
return true;
}
for(r[x]=(t?s[x]:'a');r[x]<='z';r[x]++)
{
hash[x+1]=hash[x]+r[x]*p[x];
rhash[x+1]=rhash[x]*175+r[x];
if(ok(x,d)&&ok(x,d+1)&&dfs(x+1,t&&(r[x]==s[x])))
return true;
}
return false;
} int main()
{
scanf("%d %s",&d,s);
n=strlen(s);
int i=n-1;
for(;i>=0&&s[i]=='z';i--)
s[i]='a';
if(i<0)
{
puts("Impossible");
return 0;
}
s[i]++;p[0]=1;
for(int i=1;i<n+100;i++)
p[i]=p[i-1]*175;
if(dfs(0,1)==false)
puts("Impossible");
return 0;
}
Codeforces 196 D. The Next Good String的更多相关文章
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Codeforces #196] Tutorial
Link: Codeforces #196 传送门 A: 枚举 #include <bits/stdc++.h> using namespace std; #define X first ...
- Codeforces Round #402 (Div. 2) D. String Game
D. String Game time limit per test 2 seconds memory limit per test 512 megabytes input standard inpu ...
- Educational Codeforces Round 16 E. Generate a String dp
题目链接: http://codeforces.com/problemset/problem/710/E E. Generate a String time limit per test 2 seco ...
- CodeForces Round #527 (Div3) A. Uniform String
http://codeforces.com/contest/1092/problem/A You are given two integers nn and kk. Your task is to c ...
- Codeforces 196 E. Tricky and Cleve Password
\(>Codeforces \space 196\ E. Tricky\ and\ Cleve\ Password<\) 题目大意 : 给出一个有 \(n\) 个结点,\(m\) 条边的连 ...
- Educational Codeforces Round 9 C. The Smallest String Concatenation 排序
C. The Smallest String Concatenation 题目连接: http://www.codeforces.com/contest/632/problem/C Descripti ...
- Educational Codeforces Round 8 C. Bear and String Distance 贪心
C. Bear and String Distance 题目连接: http://www.codeforces.com/contest/628/problem/C Description Limak ...
- Educational Codeforces Round 9 C. The Smallest String Concatenation —— 贪心 + 字符串
题目链接:http://codeforces.com/problemset/problem/632/C C. The Smallest String Concatenation time limit ...
随机推荐
- php angular/think angular/php模版引擎
在thinphp5中发现一个好用的模版引擎—think-angular, 此模板引擎主要特点是 不需要额外的标签定义, 全部使用属性定义, 写好的模板文件在IDE格式化代码的时候很整洁, 因为套完的模 ...
- 微信小程序资料总结
http://blog.csdn.net/ZCLengendary/article/details/54312030 --添加html标签与处理 https://www.cnblogs.com/HD ...
- 对SNL语言的解释器实现尾递归优化
对于SNL语言解释器的内容可以参考我的前一篇文章<使用antlr4及java实现snl语言的解释器>.此文只讲一下"尾递归优化"是如何实现的--"尾递归优化& ...
- NX自动出图 发布啦
经过4个月的努力,终于面世啦!!!!1.安装文件 :http://yunpan.cn/Q49TWSJmy2i5Z 请下载后,按照“安装说明.txt ”进行安装!2.学习视频:http://yun ...
- react Native环境 搭建
react Native的优点:跨平台 低投入高回报 性能高 支持动态更新.一才两用(ios和Android) 开发成本第 代码复用率高.windows环境搭建react Native开发环境1.安装 ...
- My-Eclipse 快捷键大全
Ctrl+1 快速修复(最经典的快捷键,就不用多说了) Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加) Ctrl+Alt+↑ 复制当前行到上一行(复制增加) Alt+ ...
- ASP MVC
V-view 显示层 C-controller 控制层 M-model 模型 D-database 数据库 S-Service 服务 D-Database/Dao 数据库/访问数据库的方法 View即 ...
- Qt 杂记——QTableWidget列表添加、删除(备份)
1.列表的添加 需求:向一个有两列的Table中添加一条数据 思路:新建一个inputDialog,通过按钮打开Qt自带的inputDialog,传递回输入的数据,再添加到列表中 界面: 代码: in ...
- gson序列化后整形变浮点问题解决方案
字段值是json格式的字符串.我需要将这个字段反序列化为List<Map>形式,但是在反序列化后,id变为了1.0. 百度了很多然并卵,最后改用了阿里的fastjson,没问题.(jack ...
- php第二十二节课
AJAX <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...