Uva 12206 Stammering Aliens

题目描述

输入输出格式

输入格式:

输出格式:

输入输出样例

输入样例#1:

3
baaaababababbababbab
11
baaaababababbababbab
3
cccccc
0

输出样例#1:

5 12
none
4 2

这个题后缀数组或者二分+hash好像都能做,,,但是我只是练一下后缀自动机而已hhhhh这个题我们求的显然就是right集合大小>=m的max{}的最大值,至于求最右开端的话,我们只需要记录一下right集合的最右点是哪个就行了,然后用这个减去答案长度就是最右开端。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define ll long long
#define maxn 170005
using namespace std;
int l[maxn],siz[maxn],n,r[maxn];
int cnt=1,pre=1,m,ans,pos;
int ch[maxn][26],f[maxn];
int a[maxn],c[maxn];
char s[maxn];

inline void init(){
	memset(ch,0,sizeof(ch));
	memset(f,0,sizeof(f));
	memset(c,0,sizeof(c));
	memset(siz,0,sizeof(siz));
	memset(r,0,sizeof(r));
	cnt=pre=1,l[1]=siz[1]=0;
	ans=pos=0;
}

inline void ins(int x){
	int p=pre,np=++cnt;
	pre=np,l[np]=l[p]+1;
	siz[np]=1,r[np]=l[np];

	for(;p&&!ch[p][x];p=f[p]) ch[p][x]=np;
	if(!p) f[np]=1;
	else{
		int q=ch[p][x];
		if(l[q]==l[p]+1) f[np]=q;
		else{
			int nq=++cnt;
			l[nq]=l[p]+1;
			memcpy(ch[nq],ch[q],sizeof(ch[q]));
			f[nq]=f[q];
			f[q]=f[np]=nq;
			for(;ch[p][x]==q;p=f[p]) ch[p][x]=nq;
		}
	}
}

inline void build(){
	for(int i=0;i<n;i++) ins(s[i]-‘a‘);
	for(int i=1;i<=cnt;i++) c[l[i]]++;
	for(int i=n;i>=0;i--) c[i]+=c[i+1];
	for(int i=1;i<=cnt;i++) a[c[l[i]]--]=i;
}

inline void solve(){
	for(int i=1;i<=cnt;i++){
		int now=a[i];
		siz[f[now]]+=siz[now];
		r[f[now]]=max(r[f[now]],r[now]);
		if(l[now]&&siz[now]>=m){
			if(l[now]>ans) ans=l[now],pos=r[now];
			else if(ans==l[now]&&r[now]>pos) pos=r[now];
		}
	}
}

int main(){
	while(scanf("%d",&m)==1&&m){
		scanf("%s",s),n=strlen(s);
		init();
		build();
		solve();
		if(ans) printf("%d %d\n",ans,pos-ans);
		else puts("none");
	}

	return 0;
}

  



原文地址:https://www.cnblogs.com/JYYHH/p/8456575.html

时间: 2024-08-29 09:53:15

Uva 12206 Stammering Aliens的相关文章

uva 12206 - Stammering Aliens(哈希)

题目链接:uva 12206 - Stammering Aliens 题目大意:给出一个字符串,找出至少出现m次的最长子串. 解题思路:哈希算法,将每个后缀数组建立一个哈希值,每次二分长度判断,每次判断时将哈希值排序,计数即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef unsigned long long ll; const int ma

UVA 12206 - Stammering Aliens(后缀数组)

UVA 12206 - Stammering Aliens 题目链接 题意:给定一个序列,求出出现次数大于m,长度最长的子串的最大下标 思路:后缀数组,搞出height数组后,利用二分去查找即可 这题之前还写过hash的写法也能过,不过写后缀数组的时候,犯了一个傻逼错误,把none输出成node还一直找不到...这是刷题来第二次碰到这种逗比错误了,还是得注意.. 代码: #include <cstdio> #include <cstring> #include <algori

UVA - 12206 Stammering Aliens (hash)

这题其实很容易想到2分长度,关键是2分后,怎么判断出现最多次的串是否>=m次了.这里需要用到hash来处理. 对于一个字符串   H[i] =H[i+1]*131+s[i]  ;其中 H[n]=0:那么对于一个长度为L的串 ,hanh[i,l]=H[i]-H[i+L]*x^L ; 这样记录每个字符串的hash值,然后再处理起来就比较简单了. VIEW CODE #include<cstdio> #include<algorithm> #include<iostream&

POJ 3882 Stammering Aliens 后缀数组height应用

题目来源:POJ 3882 Stammering Aliens 题意:给你m一个一个字符串 求至少出现m次的最长字符串 可以在字符串中重叠出现 思路:二分长度l 然后从height数组中找长度大于等于l的前缀 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 40010; char s[maxn]; int sa[maxn]; i

POJ 3026 Borg Maze &amp; UVA 10307 Killing Aliens in Borg Maze(BFS,最小生成树)

http://poj.org/problem?id=3026 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1248 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8498   Accepted: 2862 Description The Bor

LA 4513 Stammering Aliens 字符串hash

字符串hash模板, 本题是求,给定字符串s中至少出现m次的最长字符串长度,及此时起始位置的最大值 LA 4513  Stammering Aliens 白书p225 //#pragma warning (disable: 4786) //#pragma comment (linker, "/STACK:16777216") //HEAD #include <cstdio> #include <ctime> #include <cstdlib> #i

UVa 12206 (字符串哈希) Stammering Aliens

体验了一把字符串Hash的做法,感觉Hash这种人品算法好神奇. 也许这道题的正解是后缀数组,但Hash做法的优势就是编码复杂度大大降低. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 const int maxn = 40000 + 10; 7 const int x = 123; 8 int n, m, pos; 9 unsigne

Stammering Aliens

Time Limit: 2000MS   Memory Limit: 65536K       Description Dr. Ellie Arroway has established contact with an extraterrestrial civilization. However, all efforts to decode their messages have failed so far because, as luck would have it, they have st

uvalive 4513 Stammering Aliens

题意:给你一个串,问期中至少出现m次的最长子串及其起始位置的坐标. 思路:hash+LCP+二分答案 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 6 const int maxn = 40000 + 10; 7 const int x = 123; 8 int n, m, pos; 9 unsigned long long H[maxn], x