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

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
  
  
5 3 11 10 6 17 8
 

Sample Output
  
  
pwned wow suchproblem manystring soeasy muchlinearalgebra abcdabch
 



链接 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)
//456979
set<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;
}
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值