Codeforces 196 D. The Next Good String, codeforces196
D. The Next Good Stringtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output
In problems on strings one often has to find a string with some special 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 equalD.
You are given stringS, Consisting only of lowercase English letters. Find a good stringTWith length |S|, Consisting of lowercase English letters, which is lexicographically largerS. Of all such strings stringTMust be lexicographically minimum.
We will call a non-empty stringS[A...B] Bytes = bytesSASALifecycle + lifecycle 1...SB(1 digit ≤ DigitALimit ≤ limitBLimit ≤ limit |S|) A substring of stringSSignature = SignatureS1S2...S|S|.
A non-empty stringSSignature = SignatureS1S2...SNIs called a palindrome if for allIFrom 1NThe following fulfills:SISignature = SignatureSNAccept-Encoding-IKeys + keys 1. In other words, palindrome read the same in both directions ctions.
StringXSignature = SignatureX1X2...X|X| Is lexicographically larger than stringYSignature = SignatureY1Y2...Y|Y|, If either |X| Response> response |Y| AndX1 bytes = bytesY1, bytes,X2 bytes = bytesY2, middle..., middle ,...,X|Y| Bandwidth = bandwidthY|Y|, Or there exists such numberR(ROptional <optional |X|, Bytes,ROptional <optional |Y|), ThatX1 bytes = bytesY1, bytes,X2 bytes = bytesY2, middle..., middle ,...,XRSignature = SignatureYRAndXRLatency + latency 1 latency> latencyYRCharacter + character 1. Characters in such strings are compared like their ASCII codes.
Input
The first line contains integerD(1 digit ≤ DigitDLimit ≤ limit |S| ).
The second line contains a non-empty stringS, Its length is no more than 4 · 105 characters. The string consists of lowercase English letters.
Output
Print the good string that lexicographically followsS, Has the same length and consists of only lowercase English letters. If such string does not exist, print "Impossible" (without the quotes ).
Sample test (s) input
3aaaaaaa
Output
aabbcaa
Input
3zzyzzzz
Output
Impossible
Input
4abbabbbabbb
Output
abbbcaaabab
Search + hash,
A return string with a length of L must contain a return string with a length of L-2.
You only need to judge whether d and d + 1 are input strings.
#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;}