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 ...
随机推荐
- vue - vue
一.vue - 介绍 vue的作者叫尤雨溪,中国人.自认为很牛逼的人物,也是我的崇拜之神. 关于他本人的认知,希望大家读一下这篇关于他的文章,或许你会对语言,技术,产生浓厚的兴趣.https://mp ...
- spring boot由浅入深(一)jdk安装
1jdk说明 jdk是java提供了运行环境,简单的说就是给java语言制定了规则,并且提供了工具 jdk官网网站:http://www.oracle.com/technetwork/java/jav ...
- 把大象装进冰箱的N种方法
作者:折剑头链接:https://www.zhihu.com/question/49214119/answer/115728034来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- android GridView的setOnItemClickListener事件不执行
问题可能1: item设置的可能是button,或者可以click点击事件控件,导致控件执行而item按钮不执行 解决方法:设置控件 的 android:clickable="false& ...
- git-【六】分支的创建与合并
在版本回填退里,已经知道,每次提交,Git都把它们串成一条时间线,这条时间线就是一个分支.截止到目前,只有一条时间线,在Git里,这个分支叫主分支,即master分支.HEAD严格来说不是指向提交,而 ...
- Hook?
public interface IHook { /// <summary> /// 二维地图控件对象 /// </summary> ESRI.ArcGIS.Controls. ...
- Spring boot 开发WebService遇到的问题之一
当pom.xml文件中的配置: <artifactId>spring-boot-starter-parent</artifactId><version>2.0.6. ...
- t检验&z检验学习[转载]
转自:https://blog.csdn.net/m0_37777649/article/details/74937242 1.什么是T检验? T检验是假设检验的一种,又叫student t检验(St ...
- centos7上安装php5.6
配置yum源 追加CentOS 6.5的epel及remi源. # rpm -Uvh http://ftp.iij.ad.jp/pub/linux/fedora/epel/6/x86_64/epel- ...
- 【转】Deep Learning(深度学习)学习笔记整理系列之(七)
9.5.Convolutional Neural Networks卷积神经网络 卷积神经网络是人工神经网络的一种,已成为当前语音分析和图像识别领域的研究热点.它的权值共享网络结构使之更类似于生物神经网 ...