HDU - 4850~Wow! Such String!(dp)

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?
 

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
 

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值