HDU 4850 wow! Such string! Constructing Euler circuits

Source: Internet
Author: User

wow! Such string!Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 934 Accepted Submission (s): 318
Special Judge


Problem descriptionrecently, 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 中文版 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?
Inputthere 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.
Outputfor each case, click Output one line consisting a valid string if such a string exists, or "impossible" (without Qu OTEs) otherwise. You can output any string if there is multiple valid ones.
Sample Input
5311106178

Sample Output
Pwnedwowsuchproblemmanystringsoeasymuchlinearalgebraabcdabch



Link http://acm.hdu.edu.cn/showproblem.php?pid=4850


Test instructions: A string with an output length of n and a substring of length 4 that cannot be duplicated in lowercase letters.


Procedure: Strings with a maximum length of 4 have 26^4 altogether, if they are able to connect such as AAAA and Aaab can be connected as Aaaab. If all can be connected, the longest length is 26^4+3= 456979.

Structure, first the same structure, aaaabbbbccccdddd.....yyyyzzzz.  The presence of the existence of a 4-dimensional array such as AAAA, the dp[0][0][0][0]=1. such as Aazz the dp[0][0][25][25]=1;

Then continue to construct the next letter, starting with the largest letter Z no, because four Z has already appeared, so fill in Y.


This is the construction result of the previous part constructed by this method.

Aaaabbbbccccddddeeeeffffgggghhhhiiiijjjjkkkkllllmmmmnnnnooooppppqqqqrrrrsssstttt
Uuuuvvvvwwwwxxxxyyyyzzzzyzzyyzyzyyyxzzzxzzyxzyzxzyyxyzzxyzyxyyzxyyxxzzxxzyxxyzxx
Yxzxzxyxyxxxzxxxwzzzwzzywzzxwzyzwzyywzyxwzxzwzxywzxxwyzzwyzywyzxwyyzwyyywyyxwyxz
Wyxywyxxwxzzwxzywxzxwxyzwxyywxyxwxxzwxxywxxwwzzwwzywwzxwwyzwwyywwyxwwxzwwxywwxwz



A class of structural problems that are often seen. The problem is that it is generally required to construct a length and then a substring of a length within that length does not recur.

Read the problem, know that the theoretical basis of such problems is the Euler circuit.


#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include < malloc.h> #include <ctype.h> #include <math.h> #include <string> #include <iostream># Include <algorithm>using namespace std, #include <stack> #include <queue> #include <vector># Include <deque> #include <set> #include <sstream> #define EPS 0.00001#define LL __int64#define pi ACOs (- 1.0)//456979set<string>my;char Str[457000];int dp[26][26][26][26];int Main () {str[0]=str[1]=str[2]= ' a '; String tem;string mat;memset (str,0,sizeof str); memset (dp,0,sizeof DP); for (int i=0;i<26;i++) {for (int j=0;j<4;j+) +) {str[i*4+j]= ' a ' +i;}} for (int i=3;i<26*4;i++) {dp[str[i-3]-' a '][str[i-2]-' a '][str[i-1]-' a '][str[i]-' a ']=1;} for (int i=26*4;i<456979;i++) {for (int j=25;j>=0;j--) {if (dp[str[i-3]-' a '][str[i-2]-' a '][str[i-1]-' a '][j]==0) {dp[str[i-3]-' a '][str[i-2]-' a '][str[i-1]-' a '][j]=1;str[i]=j+ ' a '; }}int N;while (scanf ("%d", &ampN) {!=eof) {if (n>456979) {printf ("impossible"); continue;} Char tem=str[n];str[n]=0;printf ("%s\n", str); str[n]=tem;}  return 0;}





HDU 4850 wow! Such string! Constructing Euler circuits

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.