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. JavaScript计算星期几

    function zeller(dateStr) { var c = parseInt(dateStr.substr(0, 2)); var y = parseInt(dateStr.substr(2 ...

  2. c++第十四天

    <c++ primer, 5E> 第91页到第94页,笔记: 1.vector支持的操作. v.empty().v.size().v.push_back(t).v[n] 2.试图通过下标访 ...

  3. Python Web学习笔记之TCP、UDP、ICMP、IGMP的解释和区别

    TCP与UDP解释 TCP---传输控制协议,提供的是面向连接.可靠的字节流服务.当客户和服务器彼此交换数据前,必须先在双方之间建立一个TCP连接,之后才能传输数据.TCP提供超时重发,丢弃重复数据, ...

  4. STM32串口中断

    在打开串口1中断后 待机唤醒功能,(PA0唤醒)等 系统挺运行问题? ??(2014.11.13)

  5. IDEA使用技巧:CamelCasePlugin插件

    CamelCasePlugin是一款可以快速进行格式转换的工具,较常用到的是大小写转换.驼峰式转换等. 1.打开idea,然后打开设置.点击Plugins 2.快捷键shift+alt+u

  6. BZOJ 1044: [HAOI2008]木棍分割 DP 前缀和优化

    题目链接 咳咳咳,第一次没大看题解做DP 以前的我应该是这样的 哇咔咔,这tm咋做,不管了,先看个题解,再写代码 终于看懂了,卧槽咋写啊,算了还是抄吧 第一问类似于noip的那个跳房子,随便做 这里重 ...

  7. hdu2552 (浮点数复杂运算的四舍五入)题解

    三足鼎立 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  8. hdoj-2028-Lowest common multiple plus

    题目:Lowest common multiple plus 代码: #include<stdio.h> int common(int a,int b)//计算最大公约数 { int c= ...

  9. python自动制作gif并添加文字

    引言     最近租的房子快到期了,哎,因为去年是第一次找房子租,结果遇到了一个东北黑中介,押一付三,房子有啥问题,灯坏了,下水道堵了,原来签合同的时候说的客气,说是马上就会上门解决,结果实际上我每次 ...

  10. PCH Warning: header stop cannot be in a macro or #if block.

    在编写头文件时,遇到这么一个warning:PCH Warning: header stop cannot be in a macro or #if block. An intellisense PC ...