HDU-4850 wow! Such string! Constructor

Source: Internet
Author: User

Problem Description recently, Doge starts to get interested in a strange problem:whether there exists a string a followin G All the rules below:

1.The length of the string A is N.
2.The string A contains only lowercase 中文版 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 doge solve this problem? Input There is several test cases, please process till EOF.
For each test case, there'll be is one line containing one integer N (1≤n≤500000).
Sum of all N would 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. Can output any string if there is multiple valid ones. sample input5311106178 sample Outputpwnedwowsuchprob lemmanystringsoeasymuchlinearalgebraabcdabch  : Constructs a string of all lowercase letters that are known to be a substring of a length that is not less than 4 and that only occurs once. Title Analysis: A string of 26 lowercase letters with a length of 4 has a total of 26^4, that is, the string has a maximum of 26^4 seed string, so the maximum length is 26^4+3. Let's just put the longest structure on the line. The   code is as follows:
# 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\n ");        Continue        } for (int i=0;i<n;++i) printf ("%c", Ans[i]);    printf ("\ n"); } return 0;}

  

HDU-4850 wow! Such string! Constructor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.