sgu 142. Keyword 暴力,hash 难度:0
2014-10-23 23:50
495 查看
142. Keyword
time limit per test: 0.5 sec.
memory limit per test: 16384 KB
Kevin has invented a new algorithm to crypt and decrypt messages, which he thinks is unbeatable. The algorithm uses a very large key-string, out of which a keyword is found out after applying the algorithm. Then, based on this keyword, the message is easily crypted or decrypted. So, if one would try to decrypt some messages crypted with this algorithm, then knowing the keyword would be enough. Someone has found out how the keyword is computed from the large key-string, but because he is not a very experienced computer programmer, he needs your help. The key-string consists of N characters from the set {'a','b'}. The keyword is the shortest non-empty string made up of the letters 'a' and 'b', which is not contained as a contiguous substring (also called subsequence) inside the key-string. It is possible that more than one such string exists, but the algorithm is designed in such a way that any of these strings can be used as a keyword. Given the key-string, your task is to find one keyword.
Input
The first line contains the integer number N, the number of characters inside the key-string (1 <= N <= 500 000). The next line contains N characters from the set {'a','b'} representing the string.
Output
The first line of output should contain the number of characters of the keyword. The second line should contain the keyword.
Sample Input
Sample Output
time limit per test: 0.5 sec.
memory limit per test: 16384 KB
Kevin has invented a new algorithm to crypt and decrypt messages, which he thinks is unbeatable. The algorithm uses a very large key-string, out of which a keyword is found out after applying the algorithm. Then, based on this keyword, the message is easily crypted or decrypted. So, if one would try to decrypt some messages crypted with this algorithm, then knowing the keyword would be enough. Someone has found out how the keyword is computed from the large key-string, but because he is not a very experienced computer programmer, he needs your help. The key-string consists of N characters from the set {'a','b'}. The keyword is the shortest non-empty string made up of the letters 'a' and 'b', which is not contained as a contiguous substring (also called subsequence) inside the key-string. It is possible that more than one such string exists, but the algorithm is designed in such a way that any of these strings can be used as a keyword. Given the key-string, your task is to find one keyword.
Input
The first line contains the integer number N, the number of characters inside the key-string (1 <= N <= 500 000). The next line contains N characters from the set {'a','b'} representing the string.
Output
The first line of output should contain the number of characters of the keyword. The second line should contain the keyword.
Sample Input
11 aabaaabbbab
Sample Output
4 aaaa 因为长度不会超过log(n),直接暴力计算
#include <cstdio> #include <cmath> #include <cstring> using namespace std; int n; char str[500005]; bool use[1000005]; int llen[20]; int limit; bool insrt(int st,int len){ int key=0; for(int i=st;i<st+len;i++){ key<<=1; if(str[i]=='b')key+=1; } if(use[key])return false; else return use[key]=true; } char ans[21]; int main(){ scanf("%d%s",&n,str); while((1<<limit)<=n)limit++; for(int i=1;i<=limit;i++){ for(int j=0;j<=n-i;j++){ if(insrt(j,i))llen[i]++; } if(llen[i]<(1<<i)){ printf("%d\n",i); for(int j=(1<<i)-1;j>=0;j--){ if(!use[j]){ int base=1; for(int k=0;k<i;k++){ ans[i-1-k]='a'+((j&(base))?1:0); base<<=1; } break; } } break; } memset(use,0,(1<<i)); } printf("%s\n",ans); return 0; }
相关文章推荐
- SGU 142. Keyword(暴力枚举)
- hdu 1496 hash+暴力
- hdu 1496 Equations (暴力+hash)
- [BZOJ2462]矩阵模板(暴力||矩阵hash)
- FZU 2150 Fire Game 广度优先搜索,暴力 难度:0
- [数据结构]八数码(暴力,解答树,BFS+hash)
- POJ1118,Lining Up,暴力过的,hash什么的只能仰慕
- sgu 121. Bridges painting 列举情况 难度:1
- hdu 3695 10 福州 现场 F - Computer Virus on Planet Pandora 暴力 ac自动机 难度:1
- POJ 1012 Joseph 推导,暴力,约瑟夫环,打表 难度:2
- zoj 3817 Chinese Knot(hash+暴力)
- sgu108. Self-numbers 2 滚动数组 打表 难度:1
- sgu 125 Shtirlits dfs 难度:0
- sgu 113(水题 暴力)
- 快速切题 poj 2996 Help Me with the Game 棋盘 模拟 暴力 难度:0
- 【BZOJ3208】 花神的秒题计划Ⅰ 纯暴力,说好的省选难度呢?
- hdu 1496 Equations hash 暴力+hash
- POJ 3522 Slim Span 最小生成树,暴力 难度:0
- HDU 4886 TIANKENG’s restaurant(Ⅱ) (暴力+Hash)
- bzoj2795[Poi2012]A Horrible Poem 暴力hash