Wow! Such String!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1527 Accepted Submission(s): 485
Special Judge
Problem Description
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?
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.
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
5 3 11 10 6 17 8
Sample Output
pwned wow suchproblem manystring soeasy muchlinearalgebra abcdabch
Source
Recommend
liuyiding
直接跑出最长的串,如果大于这个长度输出Impossible,否则输出,由于每4个要判断一下,开一个4维数组,把每个出现的子串都标记一下,然后一个子母一个子母往里加,判断是否有子串,如果没有,则加入
#include <iostream>
#include <iomanip>
#include<stdio.h>
#include<string.h>
#include<stack>
#include<stdlib.h>
#include<queue>
#include<map>
#include<math.h>
#include<algorithm>
#include<vector>
#define LL long long
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int s[1000005],vis[26][26][26][26];
int main()
{
int l=0;
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))
{
if(n>l)
printf("Impossible\n");
else
{
for(int i=0;i<n;i++)
{
printf("%c",s[i]+97);
}
printf("\n");
}
}