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 ...
随机推荐
- SDL结合QWidget的简单使用说明
SDL(Simple DirectMeida Layer)是一个简单的封装媒体库,功能主要涉及了相关于OpenGL或者DirectX的显卡硬件功能和一些鼠标,键盘等外设访问.这里主要只说明一下它的渲染 ...
- Excel 26机制转换
[问题描述] 在Excel中,列的名称是这样一个递增序列:A.B.C.….Z.AA.AB.AC.….AZ.BA.BB.BC.….BZ.CA.….ZZ.AAA.AAB….我们需要将上述列名序列和以下自然 ...
- 自动化工具构建vue项目
其实之前对vue的话也有过一段时间的学习,博客园也是写了5篇vue的学习笔记.但是一直是通过CDN的方式在html文件头部引入vue.js,然后实例化vue对象绑定Dom,写组件写方法.就算是在实际项 ...
- MVC4中使用Html.DropDownList实现级联
本文记录了一个在MVC4中使用PartialView实现级联效果的小例子. 准备工作 首先准备一下要级联的数据,新建两个类:Province和City public class Province { ...
- conda 管理 python 版本
conda常用命令 查看当前系统下的环境 conda info -e 创建新的环境 # 指定python版本为2.7 conda create -n env_name python=2.7 # 同时安 ...
- python中读取配置文件的方式
方式1:argparse argparse,是Python标准库中推荐使用的编写命令行程序的工具.也可以用于读取配置文件. 字典样式的配置文件*.conf 配置文件test1.conf { " ...
- centos shell编程5 LANMP一键安装脚本 lamp sed lnmp 变量和字符串比较不能用-eq cat > /usr/local/apache2/htdocs/index.php <<EOF重定向 shell的变量和函数命名不能有横杠 平台可以用arch命令,获取是i686还是x86_64 curl 下载 第三十九节课
centos shell编程5 LANMP一键安装脚本 lamp sed lnmp 变量和字符串比较不能用-eq cat > /usr/local/apache2/htdocs/ind ...
- RDD的基础知识
以下的这些分析都是基于spark2.1进行的 (一)什么是RDD A Resilient Distributed Dataset (RDD), the basic abstraction in Spa ...
- centos7.3下ScyllaDB1.6安装
转自:http://10710016.blog.51cto.com/10700016/1900483 ScyllaDB 安装配置 1.说明: scylladb支持centos7.2 64位 及以上版本 ...
- rails 日期取值
获取当前时间 当前时间 Time.now UTC时间, 国际标准时间 Time.now.utc 日相关 今日开始时间00:00:00 Time.now.beginning_of_day => 2 ...