HDU-4850 Wow! Such String!(模拟) ——26行代码AC

立志用更少的代码做更高效的表达


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


题意

给你一个整数n,让你输出一个字符串。必须满足以下条件:

1.字符串的长度为n。

2.这个字符串只包含小写字母。

3.字符串中长度大于等于4的子串最多只能出现一次。

如果无解输出Impossible。


解题思路

因为大于等于4的子串只能出现一次,所以不会重复的串最长只会达到4^26+3 = 456979的长度。

因此, 我们可以预先打一个不会重复的字符表出来

若n>456979 ,则输出Impossible。

否则输出表的前n项。

打表的方式用循环即可。


代码解析

#include<iostream>
using namespace std;
int vis[26][26][26][26];
int s[5000005]; 
int main() {
	ios::sync_with_stdio;
	
	for(int i = 0; i < 26; i++) 
		s[i*4] = s[i*4+1] = s[i*4+2] = s[i*4+3] = i;
	
	for(int i = 3; i < 26*4; i++) 
		vis[s[i-3]][s[i-2]][s[i-1]][s[i]]=1;
	
	for(int i=26*4; i < 456979; i++) 	//xx
		for(int j = 25; j >= 0; j--) 
			if(vis[s[i-3]][s[i-2]][s[i-1]][j]==0) {
				vis[s[i-3]][s[i-2]][s[i-1]][j]=1;
				s[i] = j;
				break;
			}
	
	int n; while(cin>>n) {
		if(n > 456979) { cout << "Impossible" << '\n'; }
		else {
			for(int i = 0; i < n; i++) putchar(s[i]+97);
			putchar('\n');
		}
	}
	return 0;
} 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

来老铁干了这碗代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值