hdu 4850 Wow! Such String! 构造 欧拉回路

来源:互联网 发布:淘宝怎么提升排名靠前 编辑:程序博客网 时间:2024/06/06 19:07

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 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
5311106178
 

Sample Output
pwnedwowsuchproblemmanystringsoeasymuchlinearalgebraabcdabch
 



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


题意:输出长度为n,长度为4的子串不能重复出现的  小写字母组成的 字符串。


做法:最大长度为4的字符串一共有  26^4个, 如果他们都能连接  如 aaaa   和aaab   可以连接为 aaaab。  如果能都连接的话,最长  长度为26^4+3= 456979。

构造,先把 相同的 构造好, aaaabbbbccccdddd.....yyyyzzzz。把出现过的存在一个4维数组里 如 aaaa,就把dp[0][0][0][0]=1。  如 aazz 就把dp[0][0][25][25]=1;

然后继续构造下一个字母,从最大的字母开始凑接下来一个字母 z不行,因为四个z已经出现过,所以填入y。


这个是这种方法构造出来的  前面一部分的构造结果。

aaaabbbbccccddddeeeeffffgggghhhhiiiijjjjkkkkllllmmmmnnnnooooppppqqqqrrrrsssstttt
uuuuvvvvwwwwxxxxyyyyzzzzyzzyyzyzyyyxzzzxzzyxzyzxzyyxyzzxyzyxyyzxyyxxzzxxzyxxyzxx
yxzxzxyxyxxxzxxxwzzzwzzywzzxwzyzwzyywzyxwzxzwzxywzxxwyzzwyzywyzxwyyzwyyywyyxwyxz
wyxywyxxwxzzwxzywxzxwxyzwxyywxyxwxxzwxxywxxwwzzwwzywwzxwwyzwwyywwyxwwxzwwxywwxwz



经常见到的一类构造问题。这种问题普遍就是 要求 构造一段 长度  然后在这个长度内 某长度的子串没有重复出现。

看了题解,知道了这类题的理论基础是欧拉回路。


#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';break;} }}int n;while(scanf("%d",&n)!=EOF){if(n>456979){printf("Impossible");continue;}char tem=str[n];str[n]=0;printf("%s\n",str);str[n]=tem;}return 0;}