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 ...
随机推荐
- Parallel Decision Tree
Decision Tree such as C4.5 is easy to parallel. Following is an example. This is a non-parallel vers ...
- 系统管理命令之id
当我们想知道某个用户的UID和GID时id命令是非常有用的.一些程序可能需要UID/GID来运行.id使我们更加容易地找出用户的UID以GID而不必在/etc/group文件中搜寻. 1.获取指定用户 ...
- 5makefile
makefile编译多个可执行文件1: 多个 C 文件编译成不同的目标文件2: 多个 C 文件编译成 一个目标文件 注意:makefile的文件名的三种形式(优先级排序)makefile>Mak ...
- 基于SSH RSA的信任关系
RSA 非对称加密算法 client ---> server 私钥 公钥 1. 客户端生成密钥对 ssh-keygen -t rsa 执行后产生的密钥对会分别追加写到当前用户家目录下的以下文 ...
- HTTP从入门到入土(2)——HTTP协议发展历史
HTTP协议的发展历史 HTTP/0.9 0.9版本是第一个定稿的HTTP版本,相对较为简陋.它有以下特点: 只有一个命令GET 没有header等描述数据的信息 服务器发送完毕,就关闭TCP连接 注 ...
- FFmpeg 入门(5):视频同步
本文转自:FFmpeg 入门(5):视频同步 | www.samirchen.com 视频如何同步 在之前的教程中,我们已经可以开始播放视频了,也已经可以开始播放音频了,但是视频和音频的播放还未同步, ...
- JAVA volatile 解析
volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在Java 5之后,volatile关键字才得以 ...
- CSS3 Flex Box(弹性盒子)
CSS3 Flex Box(弹性盒子) 一.简介 弹性盒子是 CSS3 的一种新的布局模式. CSS3 弹性盒( Flexible Box 或 flexbox),是一种当页面需要适应不同的屏幕大小以及 ...
- Spring注解@Value
本文参考自: https://blog.csdn.net/ryelqy/article/details/77453713 @Value能让我们在java代码中使用property文件的属性,使用@Va ...
- 20145211《网络对抗》逆向及BOF基础实践
逆向及BOF基础实践——又是一年梅落时 一.实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. ...