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

      

  • 相关阅读:
    SQLSERVER查询整个数据库中某个特定值所在的表和字段的方法
    asp.net core 2.0 Json结果的格式
    Lambda表达式怎么写SQL中的in?
    jq中append()、prepend()、after()、before()的区别
    C#发送电子邮件代码记录
    Windows10系统下,彻底删除卸载MySQL
    C# DataTable 详解
    重装系统后开机时出现两个操作系统需要选择才能进入 怎样删除一个
    函数的防抖和节流
    js 实现watch监听数据变化
  • 原文地址:https://www.cnblogs.com/20143605--pcx/p/4785563.html
Copyright © 2011-2022 走看看