sgu 142. Keyword 暴力,hash 难度:0
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
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 暴力,hash 难度:0的更多相关文章
- hdu 4771 13 杭州 现场 B - Stealing Harry Potter's Precious 暴力bfs 难度:0
Description Harry Potter has some precious. For example, his invisible robe, his wand and his owl. W ...
- ZOJ 2477 Magic Cube 暴力,模拟 难度:0
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1477 用IDA*可能更好,但是既然时间宽裕数据简单,而且记录状态很麻烦,就直接 ...
- SGU 142.Keyword
时间限制:0.5s 空间限制:16M 题意 给出一个仅由'a',‘b’组成的字符串S,长度小于500 000,求一个由‘a’,‘b’组成的不是S子串的字符串T. 输出T的长度和T. Sample In ...
- Refused to execute inline event handler because it violates the following Content Security Policy directive: "xxx". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...')
/********************************************************************************* * Refused to exec ...
- NoReverseMatch at /salesman/zhuce/ Reverse for '/zhuce/' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
NoReverseMatch at /salesman/zhuce/ Reverse for '/zhuce/' with arguments '()' and keyword arguments ' ...
- Django Reverse for 'artic_post' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Reverse for 'home' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: [] ...
- POJ 3522 Slim Span 最小生成树,暴力 难度:0
kruskal思想,排序后暴力枚举从任意边开始能够组成的最小生成树 #include <cstdio> #include <algorithm> using namespace ...
- hdu 3699 10 福州 现场 J - A hard Aoshu Problem 暴力 难度:0
Description Math Olympiad is called “Aoshu” in China. Aoshu is very popular in elementary schools. N ...
- sgu 125 Shtirlits dfs 难度:0
125. Shtirlits time limit per test: 0.25 sec. memory limit per test: 4096 KB There is a checkered fi ...
随机推荐
- SmartSprites 智能批量合并 CSS 雪碧图
做前端的稍微有点经验的都知道 可以通过合并小图片 来减少请求数, 最早可能都是通过 fw.ps 等工具来手动合并, 这种方式的缺点就不吐槽了,效率低,可维护性差 等等 .... 一些很厉害的人,往往会 ...
- Django - 模型层 - 下
一.多表 sql 单表 多表 多对一 多对多 一对一 =============================================== 一对多:Book id title price p ...
- Popular Cows---poj2186(缩点,强联通)
题目链接:http://poj.org/problem?id=2186 求有多少个点满足其他n-1个点都能到达这个点,是单向图: 所以我们可以把图进行缩点,之后求出度为0的那个点内包含的点的个数就是求 ...
- busybox,alphine,ubuntu,centos/fedore操作系统
在docker 中搜索busybox docker search busybox 之后我们运行一下这个系统 Alpine操作系统 3.ubuntu 之前一直都安装过,这里不再多叙述 当时用apt- ...
- Redux 入门教程
Redux 入门教程(三):React-Redux 的用法(53@2016.09.21) Redux 入门教程(二):中间件与异步操作(32@2016.09.20) Redux 入门教程(一):基本用 ...
- 调试:Spring AOP执行过程
调试项目:https://github.com/1367356/laboratoryWeb 断点位置 点击查询:http://localhost:9002/queryNews?htmlid=15318 ...
- Spark checkpoint机制简述
本文主要简述spark checkpoint机制,快速把握checkpoint机制的来龙去脉,至于源码可以参考我的下一篇文章. 1.Spark core的checkpoint 1)为什么checkpo ...
- (转)帮你深入理解OAuth2.0协议
1. 引言 如果你开车去酒店赴宴,你经常会苦于找不到停车位而耽误很多时间.是否有好办法可以避免这个问题呢?有的,听说有一些豪车的车主就不担心这个问题.豪车一般配备两种钥匙:主钥匙和泊车钥匙.当你到酒店 ...
- HTML5游戏开发系列教程10(译)
原文地址:http://www.script-tutorials.com/html5-game-development-lesson-10/ 最后我们将继续使用canvas来进行HTML5游戏开发系列 ...
- java之throw和throws
抛出异常有三种形式,一是throw,一个throws,还有一种系统自动抛异常.下面它们之间的异同. 一.系统自动抛异常 当程序语句出现一些逻辑错误.主义错误或类型转换错误时,系统会自动抛出异常:(举个 ...