UVA12206 Stammering Aliens
思路
可以二分答案+哈希
判断有没有那个长为L的串出现至少m次即可
代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
int n,m;
const int base = 131;
unsigned long long hashx[80000],power[80000];
int ans=0,lastans=0;
char s[80000];
void init(){
memset(hashx,0,sizeof(hashx));
memset(power,0,sizeof(power));
ans=0;
lastans=0;
}
void get_hash(void){
power[0]=1;
for(int i=1;i<=n;i++){
hashx[i]=hashx[i-1]*base+s[i];
power[i]=power[i-1]*base;
}
}
unsigned long long hashs(int l,int r){
return hashx[r]-hashx[l-1]*power[r-l+1];
}
struct MapNode{
int times,pos;
bool operator < (const MapNode &b) const{
return b.times<times;
}
};
map<unsigned long long,MapNode> M;
bool check(int x){
M.clear();
ans=0;
for(int i=1;i+x-1<=n;i++){
M[hashs(i,i+x-1)].times++;
M[hashs(i,i+x-1)].pos=i;
}
bool f=false;
for(auto it = M.begin();it!=M.end();it++){
if((*it).second.times>=m){
f=true;
ans=max(ans,(*it).second.pos);
}
}
return f;
}
int main(){
// freopen("test.in","r",stdin);
// freopen("test.out","w",stdout);
while(scanf("%d",&m)==1&&m){
init();
scanf("%s",s+1);
n=strlen(s+1);
get_hash();
int l=0,r=n,t=0;
while(l<=r){
int mid=(l+r)>>1;
if(check(mid))
lastans=ans,t=mid,l=mid+1;
else
r=mid-1;
}
if(t){
printf("%d %d\n",t,lastans-1);
}
else{
printf("none\n");
}
}
return 0;
}
UVA12206 Stammering Aliens的更多相关文章
- Uva12206 Stammering Aliens 后缀数组&&Hash
Dr. Ellie Arroway has established contact with an extraterrestrial civilization. However, all effort ...
- UVA12206 Stammering Aliens 【SAM 或 二分 + hash】
题意 求一个串中出现至少m次的子串的最大长度,对于最大长度,求出最大的左端点 题解 本来想练哈希的,没忍住就写了一个SAM SAM拿来做就很裸了 只要检查每个节点的right集合大小是否不小于m,然后 ...
- UVA 12206 - Stammering Aliens(后缀数组)
UVA 12206 - Stammering Aliens 题目链接 题意:给定一个序列,求出出现次数大于m,长度最长的子串的最大下标 思路:后缀数组.搞出height数组后,利用二分去查找就可以 这 ...
- Stammering Aliens
Stammering Aliens Time Limit: 2000MS Memory Limit: 65536K Description Dr. Ellie Arroway has ...
- 题解 UVA12206 【Stammering Aliens】
终于A了这道题啊(坑啊) 教练说:这道题不能用map吧,复杂度不一个O(nlogn)吗 于是我就一直想不出来,然后看题解代码,一看就是map... 所以我就在想,那复杂度是不是也不是O(nlogn)呢 ...
- HDU4080 Stammering Aliens(二分 + 后缀数组)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4080 Description Dr. Ellie Arroway has establish ...
- UVALive - 4513 Stammering Aliens ——(hash+二分 || 后缀数组加二分)
题意:找一个出现了m次的最长子串,以及这时的最右的位置. hash的话代码还是比较好写的,,但是时间比SA多很多.. #include <stdio.h> #include <alg ...
- Hash(LCP) || 后缀数组 LA 4513 Stammering Aliens
题目传送门 题意:训练指南P225 分析:二分寻找长度,用hash值来比较长度为L的字串是否相等. #include <bits/stdc++.h> using namespace std ...
- UVa 12206 (字符串哈希) Stammering Aliens
体验了一把字符串Hash的做法,感觉Hash这种人品算法好神奇. 也许这道题的正解是后缀数组,但Hash做法的优势就是编码复杂度大大降低. #include <cstdio> #inclu ...
随机推荐
- Git 提示fatal: remote origin already exists 错误解决办法
今天使用git 添加远程github仓库的时候提示错误:fatal: remote origin already exists. 最后找到解决办法如下: 1.先删除远程 Git 仓库 $ git re ...
- js DateTime函数
---恢复内容开始--- 一.js获取当前日期时间var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFull ...
- HDU 2089 不要62【解题报告】
题目描述: 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer).杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客 ...
- vi命令复制粘贴
2.复制粘贴 yy :复制当前行 p :粘贴到光标所在行的下一行
- Azure基础(二)- 核心云服务 - Azure简介
Azure fundamentals - Core Cloud Services - Introduction to Azure Learn what Microsoft Azure is and h ...
- Linux学习路线全解,Linux操作系统学习路线
大家都知道,在现在这个信息化飞速发展的时代,IT技术火速发展,信息的重要性,可想而知.现在,在北京当一个高级运维工程师,年薪百万已经不是梦想.当然我也想,谁不想挣大钱,开好车,住好房.下面说说自己的一 ...
- ios APNS注册失败 本地push
- (void)addLocalNotice:(NSString *)titlepush { if (@available(iOS 10.0, *)) { UNUserNotificationCent ...
- laravel----------laravel一些注意事项和一些说明
1.php artisan key:generate 解释:.env文件里面的APP_KEY参数设置为一个随机字符串也就是这个key是一个随机字符串,用于实现框架中的encrypt(加密)服务, ...
- .net连接数据库递归
private void Form1_Load(object sender, EventArgs e) { List<Regions> regions = GetRegions().Whe ...
- EF(二)Model Fiirst
Model First 是先利用某些工具(如VS的EF设计器)设计出可视化的实体数据模型及他们之间的关系,然后再根据这些实体.关系去生成数据库对象及相关代码文件. 一.设计实体数据模型,生成数据库 1 ...