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
 
 
题目大意:构造出一个长度已知并且长度不小于4的子串只出现一次的全由小写字母组成的字符串。
题目分析:由26个小写字母组成的长度为4的字符串总共有26^4个,也就是说,这个字符串最多有26^4种子串,所以最长长度为26^4+3。接下来把这个最长的构造出来就行了。
 
代码如下:
# 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! (构造)的更多相关文章

  1. hdu 4850 Wow! Such String! 欧拉回路

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4080264.html 题目链接:hdu 4850 Wow! Such String! 欧拉回 ...

  2. HDU 4850 Wow! Such String!(欧拉道路)

    HDU 4850 Wow! Such String! 题目链接 题意:求50W内的字符串.要求长度大于等于4的子串,仅仅出现一次 思路:须要推理.考虑4个字母的字符串,一共同拥有26^4种,这些由这些 ...

  3. HDU 4850 Wow! Such String!

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4850 题意:给定一个N(1 ≤ N ≤ 500000),构造一个长度为N的小写字母字符串,要求所有长度大于 ...

  4. hdu 4850 Wow! Such String!(字符串处理,yy)

    题目 参考了博客http://blog.csdn.net/u013368721/article/details/37575165 //用visit[26][26][26][26]来判断新家新区的子母河 ...

  5. HDU 4849 Wow! Such City!陕西邀请赛C(最短路)

    HDU 4849 Wow! Such City! 题目链接 题意:依照题目中的公式构造出临接矩阵后.求出1到2 - n最短路%M的最小值 思路:就依据题目中方法构造矩阵,然后写一个dijkstra,利 ...

  6. HDU 5842 Lweb and String(Lweb与字符串)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  7. hdu 3553 Just a String (后缀数组)

    hdu 3553 Just a String (后缀数组) 题意:很简单,问一个字符串的第k大的子串是谁. 解题思路:后缀数组.先预处理一遍,把能算的都算出来.将后缀按sa排序,假如我们知道答案在那个 ...

  8. hdu 4893 Wow! Such Sequence!(线段树)

    题目链接:hdu 4983 Wow! Such Sequence! 题目大意:就是三种操作 1 k d, 改动k的为值添加d 2 l r, 查询l到r的区间和 3 l r. 间l到r区间上的所以数变成 ...

  9. hdu 4850 字符串构造---欧拉回路构造序列 递归+非递归实现

    http://acm.hdu.edu.cn/showproblem.php? pid=4850 题意:构造长度为n的字符序列.使得>=4的子串仅仅出现一次 事实上最长仅仅能构造出来26^4+4- ...

随机推荐

  1. 静默文件安装安装WebLogic

    一. 本文演示静默文件方式安装     •在Windows上      –打开命令行窗口 –filename.exe -mode=silent -silent_xml=file_path     •在 ...

  2. python之路----黏包的解决方案

    黏包的解决方案 远程执行命令 # server 下发命令 给client import socket sk = socket.socket() sk.bind(('127.0.0.1',8080)) ...

  3. GitHub+Hexo 搭建个人网站

    GitHub+Hexo 搭建个人网站 转自 https://www.sufaith.com/article/561.html 一.创建GitHub Pages站点 GitHub Pages是一种静态站 ...

  4. troubleshooting-When importing query results in parallel, you must specify --split-by.

    原因分析 -m 4 \ 导数命令中map task number=4,当-m 设置的值大于1时,split-by必须设置字段(需要是 int 类型的字段),如果不是 int类型的字段,则需要加上参数- ...

  5. tomcat热启动没问题, 访问项目报500解决办法

    新建maven项目 添加热启动 启动访问项目报错 报错提示 解决办法 思路:包冲突 在pom.xml中添加servlet <dependency> <groupId>javax ...

  6. .axf 转化 .bin文件 的方法

    按住shift 右击按键,进入在 X:\Program Files\Keil\MDK510\ARM\ARMCC\bin . 中打开命令cmd.exe ,然后进入一下操作. 编译自己的工程,并将&quo ...

  7. hdu 2222 Keywords Search - Aho-Corasick自动机

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  8. VC++禁止标题栏鼠标双击事件

    转载:http://blog.sina.com.cn/s/blog_4ad042e50102dwv0.html 重载DefWindowProc,在里面截获WM_NCLBUTTONDBLCLK消息进行处 ...

  9. sql 题目

    1.自增列 通用: ) from table b where b.sid<a.sid) ,* from table a; ,),* from ... 第二个已经有主键自增列的就不可以用了 还有就 ...

  10. org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Unexpected subelement profile

    原创:转载请注明出处 1.异常情况 org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Unexpected ...