Wow! Such String!

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,让你输出一个字符串。必须满足以下条件:
 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");
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值