Wow! Such String!

Wow! Such String!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 137    Accepted Submission(s): 42
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
 

Source


题目大意:

给你一个整数n,让你输出一个字符串。必须满足以下条件:
          1.字符串的长度为n。
          2.这个字符串只包含小写字母。
          3.字符串中长度大于等于4的子串最多只能出现一次。
          如果无解输出Impossible。

        开始看见这题感觉没什么,只是数据有点大而已,在比赛的时候没得到正确的答案,也没仔细去研究。

刚开始想就4个不一样,我直接随机函数,让系统去自己做,但由于概率问题,不好说。出现wa,这个方法是行不通的(对于本题)。(上次听学长讲他之前在参加一次比赛的过程中用随机函数过了一题)这个方法不深究了

       我之前想到过dfs等方法,但没实现。今天看了一个大神的题解发先我昨天想过,就没实现。我也想手动推导一下。思路就先打表,把每个序列存好,先按:aaaabbbbccccddddeeeeffffgggghhhhiiiijjjjkkkkllllmmmmnnnnooooppppqqqqrrrrss存,然后枚举所有的字母......yyyyzzzz   abcdefghijklmnopqrstuvwxyz   abdefhijlmnpqrtuvxyz   bcdfghjklnoprstvwxz  abefgijkmnoqrsuvwyz    acdeghiklmopqstuwxy  abcefgjklopqtuvyz  adefijknopstuxyz

最后发现最大长度为:26*26*26*26+3 = 456979

看见那位大牛的猜想,好像很有道理:

       可以有x个不同字符的长度大于等于m(m <= x)的子串最多只能出现一次的主串的最长长度可以达到(x ^ m) + m - 1。



#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<sstream>
#include<cassert>
using namespace std;
//#define LL __int64

#ifdef __int64
typedef __int64 LL;
#else
typedef long long LL;
#endif
#define M 500005
#define N 26
char a[M];

int b[M];
bool vis[N][N][N][N];
int l=0;

void init() {
    int ok=1;
    memset(vis,0,sizeof(vis));

    for(int i=0; i<N; i++) {
        b[l]=b[l+1]=b[l+2]=b[l+3]=i;
        l+=4;
    }
    for(int i=3; i<l; i++) {
        vis[b[i]][b[i-1]][b[i-2]][b[i-3]]=1;
    }
    while(ok) {
        ok=0;
        for(int i=0; i<N; i++) {
            if(!vis[i][b[l-1]][b[l-2]][b[l-3]]) {
                b[l]=i;
                vis[b[l]][b[l-1]][b[l-2]][b[l-3]]=1;
                ok=1;
                l++;
            }
        }
    }
}

int main() {
    init();
    int n;
    while(cin>>n){
        if(n>l){
            puts("Impossible");
        }
        else{
            for(int i=0;i<n;i++){
                printf("%c",b[i]+97);
            }
            printf("\n");
        }
    }
    return 0;
}

/*
void init() {
    for(int i=0; i<=456979; i++) {
        int t=rand()%26;
        int t1=rand()%26;
        char c=(t+t1)/2+'a';
        a[i]=c;
    }
}

int main() {
    init();
    int n;
    while(scanf("%d",&n)!=EOF) {
        if(n>456979) {
            puts("Impossible");
        } else {
            for(int i=0; i<n; i++) {
                printf("%c",a[i]);
            }
            printf("\n");
        }
    }
    return 0;
}
*/




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值