Recently, doge starts to get interested in a strange problem: whether there exists a string A following all the rules below:
1.The length of the string A is N .
2.The string A contains only lowercase English alphabet letters.
3.Each substring of A with length equal to or larger than 4 can appear in the string exactly once.
Doge cannot solve the problem, so he turns to his brother Yuege for help. However, Yuege is busy setting problems. Would you please help doge solve this problem? Input
There are several test cases, please process till EOF.
For each test case, there will be one line containing one integer N (1 ≤ N ≤ 500000).
Sum of all N will not exceed 5000000.
Output
For each case, please output one line consisting a valid string if such a string exists, or “Impossible” (without quotes) otherwise. You can output any string if there are multiple valid ones.
Sample Input
Sample Output
1.The length of the string A is N .
2.The string A contains only lowercase English alphabet letters.
3.Each substring of A with length equal to or larger than 4 can appear in the string exactly once.
Doge cannot solve the problem, so he turns to his brother Yuege for help. However, Yuege is busy setting problems. Would you please help doge solve this problem? Input
For each test case, there will be one line containing one integer N (1 ≤ N ≤ 500000).
Sum of all N will not exceed 5000000.
5 3 11 10 6 17 8
pwned wow suchproblem manystring soeasy muchlinearalgebra abcdabch
题目大意:给你一个整数n,让你输出一个字符串。必须满足以下条件:
1.字符串的长度为n。
2.这个字符串只包含小写字母。
3.字符串中长度大于等于4的子串最多只能出现一次。
如果无解输出Impossible。
做法:最大长度为4的字符串一共有 26^4个, 如果他们都能连接 如 aaaa 和aaab 可以连接为 aaaab。 如果能都连接的话,最长 长度为26^4+3= 456979。
构造,先把 相同的 构造好, aaaabbbbccccdddd.....yyyyzzzz。把出现过的存在一个4维数组里 如 aaaa,就把dp[0][0][0][0]=1。 如 aazz 就把dp[0][0][25][25]=1;
每次每增加一个字母时从a~z for一遍,看这个字母会不会和前三个字母构成的子串在之前的串中已经出现,如果未出现过,那么这个位置上就决定是这个字母了,然后下个位置直接从接下去的字母中选取;如果已经出现相同子串则枚举下个字母,如果一直到z还是不能添加一个字母到本位置但是这次a~z中已经有至少一个添加到之前的位置上时,重头再for,否则结束循环(因为已经枚举完所有的26个字母且都已经与前面三个字母构成的子串出现过了)。
#include<stdio.h> #include<iostream> #include<string.h> using namespace std; int vis[27][27][27][27]; int s[1000000]; int main() { int l=0; memset(vis,0,sizeof(vis)); for(int i=0;i<26;i++) { s[l]=s[l+1]=s[l+2]=s[l+3]=i; l+=4; } for(int i=3;i<l;i++) { vis[s[i-3]][s[i-2]][s[i-1]][s[i]]=1; } int p=1; while(p) { p=0; for(int i=0;i<26;i++) { if(!vis[s[l-3]][s[l-2]][s[l-1]][i]) { s[l]=i; vis[s[l-3]][s[l-2]][s[l-1]][s[l]]=1; p=1; l++; } } } int n; while(scanf("%d",&n)!=-1) { if(n>l) { printf("Impossible\n"); } else { for(int i=0;i<n;i++) { printf("%c",s[i]+97); } printf("\n"); } } }