[POJ3693]Maximum repetition substring

试题描述

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 string containing lowercase letters, you are to find a substring of it with maximum repetition number.

输入

The input consists of multiple test cases. Each test case contains exactly one line, which
gives a non-empty string consisting of lowercase letters. The length of the string will not be greater than 100,000.

The last test case is followed by a line containing a ‘#‘.

输出

For each test case, print a line containing the test case number( beginning with 1) followed by the substring of maximum repetition number. If there are multiple substrings of maximum repetition number, print the lexicographically smallest one.

输入示例

ccabababc
daabbccaa
#

输出示例

Case 1: ababab
Case 2: aa

数据规模及约定

见“输入

题解

这题好神。。。

注意到如果存在一个子串是由长度为 L 的串重复得来,那么我们每隔 L 取一个字符一定能取到所有周期中的一个字符。这样的话就可以利用调和级数枚举 L 的大小,然后取 L 的整倍数的位置,然后再统计统计更新更新就好了。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>

#define maxn 200010
#define maxlog 20

int Log[maxn];

struct Suf {
	char S[maxn];
	int n, rank[maxn], height[maxn], sa[maxn], Ws[maxn];
	int mnr[maxlog][maxn], mnh[maxlog][maxn];

	Suf() {}
	void init(const char* Str) {
		memset(S, 0, sizeof(S));
		strcpy(S + 1, Str);
		n = strlen(S + 1);
	}

	bool cmp(int* a, int p1, int p2, int l) {
		if(p1 + l > n && p2 + l > n) return a[p1] == a[p2];
		if(p1 + l > n || p2 + l > n) return 0;
		return a[p1] == a[p2] && a[p1+l] == a[p2+l];
	}

	void ssort() {
		int *x = rank, *y = height;
		int m = 0;
		memset(Ws, 0, sizeof(Ws));
		for(int i = 1; i <= n; i++) Ws[x[i] = S[i]]++, m = std::max(m, x[i]);
		for(int i = 1; i <= m; i++) Ws[i] += Ws[i-1];
		for(int i = n; i; i--) sa[Ws[x[i]]--] = i;
		for(int j = 1, pos = 0; pos < n; j <<= 1, m = pos) {
			pos = 0;
			for(int i = n - j + 1; i <= n; i++) y[++pos] = i;
			for(int i = 1; i <= n; i++) if(sa[i] > j) y[++pos] = sa[i] - j;
			for(int i = 1; i <= m; i++) Ws[i] = 0;
			for(int i = 1; i <= n; i++) Ws[x[i]]++;
			for(int i = 1; i <= m; i++) Ws[i] += Ws[i-1];
			for(int i = n; i; i--) sa[Ws[x[y[i]]]--] = y[i];
			std::swap(x, y); pos = 1; x[sa[1]] = 1;
			for(int i = 2; i <= n; i++) x[sa[i]] = cmp(y, sa[i], sa[i-1], j) ? pos : ++pos;
		}
		return ;
	}

	void calch() {
		for(int i = 1; i <= n; i++) rank[sa[i]] = i;
		for(int i = 1, j, k = 0; i <= n; height[rank[i++]] = k)
			for(k ? k-- : 0, j = sa[rank[i]-1]; S[j+k] == S[i+k]; k++);
		return ;
	}

	void debug() {
		for(int i = 1; i <= n; i++) printf("%d%c", sa[i], i < n ? ‘ ‘ : ‘\n‘);
		for(int i = 1; i <= n; i++) printf("%d%c", height[i], i < n ? ‘ ‘ : ‘\n‘);
		return ;
	}

	void rmq_init() {
		Log[1] = 0;
		for(int i = 2; i <= n; i++) Log[i] = Log[i>>1] + 1;
		for(int i = 1; i <= n; i++) mnr[0][i] = rank[i], mnh[0][i] = height[i];
		for(int j = 1; (1 << j) <= n; j++)
			for(int i = 1; i + (1 << j) - 1 <= n; i++) {
				mnr[j][i] = std::min(mnr[j-1][i], mnr[j-1][i+(1<<j-1)]);
				mnh[j][i] = std::min(mnh[j-1][i], mnh[j-1][i+(1<<j-1)]);
			}
		return ;
	}

	int qrnk(int l, int r) {
		int t = Log[r-l+1];
		return std::min(mnr[t][l], mnr[t][r-(1<<t)+1]);
	}

	int qhei(int l, int r) {
		if(l > r) std::swap(l, r); l++;
		int t = Log[r-l+1];
		return std::min(mnh[t][l], mnh[t][r-(1<<t)+1]);
	}
} sol1, sol2;

char inS[maxn];

int main() {
//	freopen("data.in", "r", stdin);
//	freopen("data.out", "w", stdout);
	int kase = 0;
	while(~scanf("%s", inS)) {
		if(inS[0] == ‘#‘) break;
		sol1.init(inS);
		for(int i = 0; i < (sol1.n >> 1); i++) std::swap(inS[i], inS[sol1.n-i-1]);
		sol2.init(inS);

		int n = sol1.n;
		sol1.ssort(); sol1.calch(); sol1.rmq_init();
		sol2.ssort(); sol2.calch(); sol2.rmq_init();
//		sol1.debug(); sol2.debug();
		int st = sol1.sa[sol1.qrnk(1,n)], len = 1, rpt = 1;
		for(int L = 1; L <= n; L++) {
			for(int i = L; i + L <= n; i += L) {
				if(sol1.S[i] == sol1.S[i+L]) {
					int r = sol1.qhei(sol1.rank[i], sol1.rank[i+L]) - 1,
						l = sol2.qhei(sol2.rank[n-i+1], sol2.rank[n-i-L+1]) - 1,
						tmp = r / L + 1 + l / L + 1;
					if(rpt < tmp) rpt = tmp, st = sol1.sa[sol1.qrnk(i-l,i-l+(l+r+1)%L)], len = rpt * L;
					else if(rpt == tmp) {
						int pos = sol1.sa[sol1.qrnk(i-l,i-l+(l+r+1)%L)];
						if(sol1.rank[st] > sol1.rank[pos]) st = pos, len = rpt * L;
					}
				}
			}
		}

		printf("Case %d: ", ++kase);
		for(int i = st; i <= st + len - 1; i++) putchar(sol1.S[i]); putchar(‘\n‘);
	}

	return 0;
}
时间: 2024-11-10 14:37:29

[POJ3693]Maximum repetition substring的相关文章

poj3693 Maximum repetition substring 后缀数组

http://poj.org/problem?id=3693 Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7241   Accepted: 2162 Description The repetition number of a string is defined as the maximum number R such that the string can b

POJ3693:Maximum repetition substring(后缀数组+RMQ)

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

poj 3693 Maximum repetition substring(后缀数组)

题目链接:poj 3693 Maximum repetition substring 题目大意:求一个字符串中循环子串次数最多的子串. 解题思路:对字符串构建后缀数组,然后枚举循环长度,分区间确定.对于一个长度l,每次求出i和i+l的LCP,那么以i为起点,循环子串长度为l的子串的循环次数为LCP/l+1,然后再考虑一下从i-l+1~i之间有没有存在增长的可能性. #include <cstdio> #include <cstring> #include <vector>

POJ 3693 Maximum repetition substring (后缀数组)

题目大意: 求出字典序最小,重复次数最多,的子串. 思路分析: RMQ + height 数组可以求出任意两个后缀的lcp 我们枚举答案字符串的重复的长度. 如果这个字符串的长度为 l ,而且这个字符串出现过两次或两次以上 那么你会发现在原串中  str[0] str[l] str[2*l] ....肯定有相邻的两个被包含在重复的串中. 我们求出这两个相邻的后缀的lcp 我们上面仅仅说的是被包含在重复的串中,但并不一定就是以 str[0], str[l],str[2*l]....为起点的. 那我

Maximum repetition substring 后缀数组

Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7578   Accepted: 2281 Description The repetition number of a string is defined as the maximum number R such that the string can be partitioned into R same conse

POJ 3693 Maximum repetition substring(后缀数组神题)

POJ 3693 Maximum repetition substring 题目链接 题意:给定一个字符串,求出其子串中,重复次数最多的串,如果有相同的,输出字典序最小的 思路:枚举长度l,把字符串按l分段,这样对于长度为l的字符串,肯定会包含一个分段位置,这样一来就可以在每个分段位置,往后做一次lcp,求出最大匹配长度,然后如果匹配长度有剩余,看剩余多少,就往前多少位置再做一次lcp,如果匹配出来长度更长,匹配次数就加1,这样就可以枚举过程中保存下答案了 这样问题还有字典序的问题,这个完全可以

POJ 3693 Maximum repetition substring (寻找重复次数最多的连续子串)

Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9083   Accepted: 2782 Description The repetition number of a string is defined as the maximum number R such that the string can be partitioned into R same conse

hdu 2459 Maximum repetition substring(后缀数组)

题目链接:hdu 2459 Maximum repetition substring 题意: 让你找一个重复最多的子串,并且输出. 题解: 这个是论文题,看的cxlove的题解,不是很理解为什么这样就能完全找完,当作结论使吧. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 using namespace std; 4 namespace suffixarray{ 5 #define FN(n) f

POJ 3693 Maximum repetition substring(后缀数组+RMQ)

Maximum repetition substring 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&quo