HDU-4850 Wow! Such String! (构造)

Wow! Such String!

http://acm.hdu.edu.cn/showproblem.php?pid=4850
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
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?
 

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
   
   
   
   
5 3 11 10 6 17 8
 

Sample Output
   
   
   
   
pwned wow suchproblem manystring soeasy muchlinearalgebra abcdabch

题目大意:构造一个长度为n的字符串,使任意长度为4以及更大的子串不重复?

可以发现:如果不存在重复的长度为4的子串,则也不存在长度更长的重复的子串,共26^4各不同的子串,则能构造出的最长的字符串长度为26^4+3

所以在构造第i个字符时,可以判断i-3,i-2,i-1这三个字符,记录以这三个字符为前缀的子串的最后一个字符构造到哪一个字符,每次+1,即可构造出不同的

初始化前三个字符均为a时,只能构造很短的字符串,而初始化为z时,就能构造出全部的字符串

第一次impossible没有输出换行,结果WA了,找了半天错。。。

#include <cstdio>
#include <cstring>

using namespace std;

int lim,n,inf=26*26*26*26+3;
char cur[129][129][129];
char s[5],t;

int main() {
    while(1==scanf("%d",&n)) {
       if(n>inf) {
            printf("Impossible\n");
            continue;
        }
        s[0]='z';
        s[1]='z';
        s[2]='z';
        for(int i='a';i<='z';++i) {
            for(int j='a';j<='z';++j) {
                for(int k='a';k<='z';++k) {
                    cur[i][j][k]='a';
                }
            }
        }
        lim=3<n?3:n;
        for(int i=0;i<lim;++i) {
            printf("%c",s[i]);
        }
        for(int i=lim;i<n;++i) {
            s[(i-1)%3]=cur[s[(i-3)%3]][s[(i-2)%3]][s[(i-1)%3]]++;
            printf("%c",s[(i-1)%3]);
        }
        printf("\n");
    }
    return 0;
}


你可能感兴趣的:(HDU,构造)