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

65 篇文章 0 订阅

 

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;
}

以上代码虽然也能AC,但是与解题思路不符。

原本是根据前三个字符获取当前字符的值,结果我赋值到上一个字符去了。。。

改正后代码如下

#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%3]=cur[s[(i-3)%3]][s[(i-2)%3]][s[(i-1)%3]]++;
            printf("%c",s[i%3]);
        }
        printf("\n");
    }
    return 0;
}

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值