zoukankan      html  css  js  c++  java
  • HDU-4850 Wow! Such String! (构造)

    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
     
     
    题目大意:构造出一个长度已知并且长度不小于4的子串只出现一次的全由小写字母组成的字符串。
    题目分析:由26个小写字母组成的长度为4的字符串总共有26^4个,也就是说,这个字符串最多有26^4种子串,所以最长长度为26^4+3。接下来把这个最长的构造出来就行了。
     
    代码如下:
    # include<iostream>
    # include<cstdio>
    # include<map>
    # include<string>
    # include<cstring>
    # include<algorithm>
    using namespace std;
    const int N=26*26*26*26+3;
    int vis[26][26][26][26],n;
    char ans[N+10];
    void get_ans()
    {
        memset(vis,0,sizeof(vis));
        int pos=0;
        for(char a='a';a<='z';++a)
            ans[pos++]=a,ans[pos++]=a,ans[pos++]=a,ans[pos++]=a;
        for(int i=3;i<104;++i)
            vis[ans[i-3]-'a'][ans[i-2]-'a'][ans[i-1]-'a'][ans[i]-'a']=1;
        char a='z';
        while(pos<456979){
            int cnt=0;
            for(int c=a+1;cnt<2;++c){
                if(c>'z'){
                    c='a';
                    ++cnt;
                }
                if(vis[ans[pos-3]-'a'][ans[pos-2]-'a'][ans[pos-1]-'a'][c-'a'])
                    continue;
                vis[ans[pos-3]-'a'][ans[pos-2]-'a'][ans[pos-1]-'a'][c-'a']=1;
                ans[pos++]=c;
                a=c;
                break;
            }
        }
    }
    int main()
    {
        get_ans();
        while(~scanf("%d",&n))
        {
            if(n>N){
                printf("Impossible
    ");
                continue;
            }
            for(int i=0;i<n;++i)
                printf("%c",ans[i]);
            printf("
    ");
        }
        return 0;
    }
    

      

  • 相关阅读:
    NOJ 1116 哈罗哈的大披萨 【淡蓝】 状态压缩DP
    优先队列原理与实现【转】
    testC-I
    济南NOIP冬令营 选拔(select)
    P4747 D’s problem(d)
    P4746 C’s problem(c)
    P4745 B’s problem(b)
    P4744 A’s problem(a)
    [bzoj] 1004: [HNOI2008]Cards
    NOIP2013 表达式求值
  • 原文地址:https://www.cnblogs.com/20143605--pcx/p/4785563.html
Copyright © 2011-2022 走看看