uvalive 4513 Stammering Aliens
题意:给你一个串,问期中至少出现m次的最长子串及其起始位置的坐标。
思路:hash+LCP+二分答案
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = + ;
const int x = ;
int n, m, pos;
unsigned long long H[maxn], xp[maxn]; unsigned long long hash[maxn];
int rank[maxn]; int cmp(const int& a, const int& b)
{
return hash[a] < hash[b] || (hash[a] == hash[b] && a < b);
} int possible(int L)
{
int c = ;
pos = -;
for(int i = ; i < n-L+; i++)
{
rank[i] = i;
hash[i] = H[i] - H[i+L]*xp[L];
}
sort(rank, rank+n-L+, cmp);
for(int i = ; i < n-L+; i++)
{
if(i == || hash[rank[i]] != hash[rank[i-]])
c = ;
if(++c >= m)
pos = max(pos, rank[i]);
}
return pos >= ;
} int main()
{
char s[maxn];
while(scanf("%d", &m) == && m)
{
scanf("%s", s);
n = strlen(s);
H[n] = ;
for(int i = n-; i >= ; i--)
H[i] = H[i+]*x + (s[i] - 'a');
xp[] = ;
for(int i = ; i <= n; i++)
xp[i] = xp[i-]*x;
if(!possible())
printf("none\n");
else
{
int L = , R = n+;
while(R - L > )
{
int M = L + (R-L)/;
if(possible(M))
L = M;
else R = M;
}
possible(L);
printf("%d %d\n", L, pos);
}
}
return ;
}
uvalive 4513 Stammering Aliens的更多相关文章
- 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(后缀数组)
UVA 12206 - Stammering Aliens 题目链接 题意:给定一个序列,求出出现次数大于m,长度最长的子串的最大下标 思路:后缀数组.搞出height数组后,利用二分去查找就可以 这 ...
- Stammering Aliens
Stammering Aliens Time Limit: 2000MS Memory Limit: 65536K Description Dr. Ellie Arroway has ...
- Uva12206 Stammering Aliens 后缀数组&&Hash
Dr. Ellie Arroway has established contact with an extraterrestrial civilization. However, all effort ...
- HDU4080 Stammering Aliens(二分 + 后缀数组)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4080 Description Dr. Ellie Arroway has establish ...
- UVa 12206 (字符串哈希) Stammering Aliens
体验了一把字符串Hash的做法,感觉Hash这种人品算法好神奇. 也许这道题的正解是后缀数组,但Hash做法的优势就是编码复杂度大大降低. #include <cstdio> #inclu ...
- 【HDOJ】4080 Stammering Aliens
1. 题目描述给定一个长为$n \in [1, 4000]$的字符串,求其中长度最长的子串,并且该子串在原串中出现至少$m$次,并求最右起始位置. 2. 基本思路两种方法:二分+后缀数组,或者二分+哈 ...
- uva 12206 - Stammering Aliens
基于hash的LCP算法: #include<cstdio> #include<cstring> #include<algorithm> #define maxn ...
随机推荐
- string.Equals 比较2个字符串是否相同忽略大小写
bool res = string.Equals(str1, str2, StringComparison.CurrentCultureIgnoreCase)
- PAT-乙级-1038. 统计同成绩学生(20)
1038. 统计同成绩学生(20) 时间限制 250 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求读入N名学生的成绩,将 ...
- Scala中的Extractor
Scala中使用unapply方法可以实现三种extractor(另外使用unapplySeq也可以实现extractor) def unapply(object: S): Option[(T1, . ...
- 网站404,500错误页面的处理,及500异常写入errorLog日志
1.web.xml 配置 <error-page> <error-code>404</error-code> <location>/404.jsp< ...
- 【NOIP 2016 总结】
距离杯赛已经很久了,然而我现在才打总结.. 我好惨的说..两场才380... DAY 1 第一题 toy 送分题,模拟的时候+一下再mod一下就好. [当时打完这题就没再看一眼了,好方的说] #inc ...
- [itint5]摆放窗口
http://www.itint5.com/oj/#47 一种做法是:把矩形所占的方格都设为-1,就是个最大子矩阵和问题.复杂度o(w^2*h)或o(w*h^2),空间W*H猜想应用场景是:电脑屏幕上 ...
- Linux下Keepalived 安装与配置
Keepalived 安装与配置 一.环境说明 1.操作系统内核版本:2.6.9-78.ELsmp 2.Keepalived软件版本:keepalived-1.1.20.tar.gz 二.环境配置 1 ...
- Armitage主屏幕说明与命令行启动
(1)我们将Armitage主屏幕标注为A.B和C A:该区域显示预配置的模块.您可以在模块列表下面的文本框中输入要查找的模块进行查找. B:该区域显示我们可以进行漏洞测试的活跃主机. C:该区域显示 ...
- Creating a web server in pure C(c/c++ 写web server)
一个简单的例子:https://github.com/fekberg/GoHttp 一个运行在windows上例子:http://www.adp-gmbh.ch/win/misc/webserver. ...
- eclipse如何导入PHP的项目
http://zhidao.baidu.com/link?url=2jvsgawRlEWzE63-Wm-e51_Nl0dWH1Z4z5VS_s2E824y2fYqsvNzdZ8GfEh6DOVtjY8 ...