UVA 题目1223 - Editor(后缀数组求出现次数超过两次的最长子串的长度)

Mr. Kim is a professional programmer. Recently he wants to design a new editor which has as many functions as possible. Most editors support a simple search function that finds one occurrence (or all occurrences
successively) of a query pattern string in the text.

He observed that the search function in commercial editors does nothing if no query pattern is given. His idea of a new search function regards each substring of the given text as a query
pattern string itself and his new function finds another occurrence in the text. The problem is that there can be occurrences of many substrings in the text. So, Mr. Kim decides that the new function finds only occurrences of the longest
substring in the text in order to remedy the problem. A formal definition of the search function is as follows:

Given a text string S , find the longest substring in text string S such that the substring appears at least twice. The two occurrences are allowed
to overlap.

Input

Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the
input. For each test case, a text string S is given in one line. For every string, the length is less than or equal to 5,000 and the alphabet  is
the set of lowercase English characters.

Output

Your program is to write to standard output. Print exactly one line for each test case. Print the length of the longest substring in text string S such that the substring appears
at least twice.

Sample Input

3
abcdefghikjlmn
abcabcabc
abcdabcabb

Sample Output

0
6 

3

做这么多后缀数组题以来,这个应该是最水的了吧

ac代码

0ms过
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b?a:b)
#define N 1000005
using namespace std;
char str[5010];
int sa[5010],Rank[5010],rank2[5010],height[5010],c[5010],*x,*y,s[5010],k;
void cmp(int n,int sz)
{
    int i;
    memset(c,0,sizeof(c));
    for(i=0;i<n;i++)
        c[x[y[i]]]++;
    for(i=1;i<sz;i++)
        c[i]+=c[i-1];
    for(i=n-1;i>=0;i--)
        sa[--c[x[y[i]]]]=y[i];
}
void build_sa(char *s,int n,int sz)
{
    x=Rank,y=rank2;
    int i,j;
    for(i=0;i<n;i++)
        x[i]=s[i],y[i]=i;
    cmp(n,sz);
    int len;
    for(len=1;len<n;len<<=1)
    {
        int yid=0;
        for(i=n-len;i<n;i++)
        {
            y[yid++]=i;
        }
        for(i=0;i<n;i++)
            if(sa[i]>=len)
                y[yid++]=sa[i]-len;
            cmp(n,sz);
        swap(x,y);
        x[sa[0]]=yid=0;
        for(i=1;i<n;i++)
        {
            if(y[sa[i-1]]==y[sa[i]]&&sa[i-1]+len<n&&sa[i]+len<n&&y[sa[i-1]+len]==y[sa[i]+len])
                x[sa[i]]=yid;
            else
                x[sa[i]]=++yid;
        }
        sz=yid+1;
        if(sz>=n)
            break;
    }
    for(i=0;i<n;i++)
        Rank[i]=x[i];
}
void getHeight(char *s,int n)
{
    int k=0;
    for(int i=0;i<n;i++)
    {
        if(Rank[i]==0)
            continue;
        k=max(0,k-1);
        int j=sa[Rank[i]-1];
        while(s[i+k]==s[j+k])
            k++;
        height[Rank[i]]=k;
    }
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%s",str);
		int ans=0;
		int n=strlen(str);
		build_sa(str,n+1,128);
		getHeight(str,n);
		for(int i=1;i<=n;i++)
			ans=max(ans,height[i]);
		printf("%d\n",ans);
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-02 21:25:55

UVA 题目1223 - Editor(后缀数组求出现次数超过两次的最长子串的长度)的相关文章

POJ - 3693 Maximum repetition substring(后缀数组求重复次数最多的连续重复子串)

Description The repetition number of a string is defined as the maximum number R such that the string can be partitioned into R same consecutive substrings. For example, the repetition number of "ababab" is 3 and "ababa" is 1. Given a

UVA 题目11512 - GATTACA(后缀数组求出现次数最多的子串及重复次数)

The Institute of Bioinformatics and Medicine (IBM) of your country has been studying the DNA sequences of several organisms, including the human one. Before analyzing the DNA of an organism, the investigators must extract the DNA from the cells of th

poj 3693 后缀数组求重复次数最多的连续重复子串

#include<iostream> #include<cstring> #include<set> #include<map> #include<cmath> #include<stack> #include<queue> #include<deque> #include<list> #include<algorithm> #include<stdio.h> #includ

UVA 12206 - Stammering Aliens(后缀数组)

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

Uva 12012 Detection of Extraterrestrial 求循环节个数为1-n的最长子串长度 KMP

题目链接:点击打开链接 题意: 给定一个字符串str 求字符串str的 循环节个数为 1-len 个的 最长子串长度 思路:套用kmp的性质 #include<string.h> #include<stdio.h> #include <iostream> using namespace std; #define n 1300 void getnext(char str[n],int next[n]){ int m=strlen(str); next[0]=next[1]

POJ3294--Life Forms 后缀数组+二分答案 大于k个字符串的最长公共子串

Life Forms Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10800   Accepted: 2967 Description You may have wondered why most extraterrestrial life forms resemble humans, differing by superficial traits such as height, colour, wrinkles, e

LeetCode: 3_Longest Substring Without Repeating Characters | 求没有重复字符的最长子串的长度 | Medium

题目: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest s

算法学习(十一)数组中出现次数超过长度一半的数

数组中出现次数超过数组长度一半的 数字 题目描述: 给定 一个数组,找到数组中出现次数超过数组长度一半的数字,如数组 a[]= {0,1,2,1,1},输出为1 分析: 1,可以使用伴随数组b,遍历数组a,将值作为数组b的下标,将次数作为值,然后遍历数组b,找到次数超过一半的值,然后输出.时间复杂度为O(N),不过要增加空间复杂度. 2,我们可以先对数组进行排序,因为某个数字出现次数超过一半,所以在数组的N/2处,就一定是那个数字.时间复杂主要为排序的时间,使用快排O (N*logN). 3,有

HDOJ 题目4416 Good Article Good sentence(后缀数组求a串子串在b串中不出现的种类数)

-每周六晚的BestCoder(有米!) Good Article Good sentence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2784    Accepted Submission(s): 785 Problem Description In middle school, teachers used to encour