题意:求一个串中出现重复子串次数最多的数目。

析:枚举每个长度的子串,至少要重复两次,必然会经过s[l*i]中相邻的两个,然后再分别向前和向后匹配即可。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 50000 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} struct Array{
int sa[maxn], s[maxn], t[maxn], t2[maxn];
int r[maxn], h[maxn], c[maxn], dp[maxn][20];
int n; void init(){
n = 0; memset(sa, 0, sizeof sa);
} void build_sa(int m){
int *x = t, *y = t2;
for(int i = 0; i < m; ++i) c[i] = 0;
for(int i = 0; i < n; ++i) ++c[x[i] = s[i]];
for(int i = 1; i < m; ++i) c[i] += c[i-1];
for(int i = n-1; i >= 0; --i) sa[--c[x[i]]] = i; for(int k = 1; k <= n; k <<= 1){
int p = 0;
for(int i = n-k; i < n; ++i) y[p++] = i;
for(int i = 0; i < n; ++i) if(sa[i] >= k) y[p++] = sa[i] - k;
for(int i = 0; i < m; ++i) c[i] = 0;
for(int i = 0; i < n; ++i) ++c[x[y[i]]];
for(int i = 1; i < m; ++i) c[i] += c[i-1];
for(int i = n-1; i >= 0; --i) sa[--c[x[y[i]]]] = y[i]; swap(x, y);
p = 1; x[sa[0]] = 0;
for(int i = 1; i < n; ++i)
x[sa[i]] = y[sa[i-1]] == y[sa[i]] && y[sa[i-1]+k] == y[sa[i]+k] ? p-1 : p++;
if(p >= n) break;
m = p;
}
} void getHight(){
int k = 0;
for(int i = 0; i < n; ++i) r[sa[i]] = i;
for(int i = 0; i < n; ++i){
if(k) --k;
int j = sa[r[i]-1];
while(s[i+k] == s[j+k]) ++k;
h[r[i]] = k;
}
} void rmq_init(){
for(int i = 1; i <= n; ++i) dp[i][0] = h[i];
for(int j = 1; (1<<j) <= n; ++j)
for(int i = 1; i + (1<<j) <= n; ++i)
dp[i][j] = min(dp[i][j-1], dp[i+(1<<j-1)][j-1]);
} int query(int L, int R){
L = r[L]; R = r[R];
if(L > R) swap(L, R);
++L;
int k = log(R-L+1) / log(2.0);
return min(dp[L][k], dp[R-(1<<k)+1][k]);
}
};
Array arr;
char s[5]; int main(){
int T; cin >> T;
while(T--){
scanf("%d", &n);
arr.init();
for(int i = 0; i < n; ++i){
scanf("%s", s);
arr.s[arr.n++] = s[0] - 'a' + 1;
}
arr.s[arr.n++] = 0;
arr.build_sa(5);
arr.getHight();
arr.rmq_init();
int ans = 0;
for(int i = 1; i <= n; ++i)
for(int j = 0; j + i <= n; j += i){
int k = arr.query(j, j + i);
int res = k / i + 1;
int t = j - (i - k % i);
if(t >= 0 && arr.query(t, t + i) >= k) ++res;
ans = max(ans, res);
}
printf("%d\n", ans);
}
return 0;
}

  

最新文章

  1. C# 模拟webform里面按钮的点击事件
  2. PAT 1001. 害死人不偿命的(3n+1)猜想 (15)
  3. iOS开发之AFNetworking 3.0.4使用-OC
  4. XNote Ver:0.79
  5. 从hadoop框架与MapReduce模式中谈海量数据处理
  6. Windows Service的安装卸载 和 Service控制(转)
  7. c# 操作 MongoDB 的 第三方类库 MongoRepository
  8. jQuery Validation让验证变得如此容易(三)
  9. EntityFramework Core 自动绑定模型映射
  10. deb包转化为rpm包
  11. Hibernate(4)简单的HelloWorld
  12. CentOS下的yum upgrade和yum update区别
  13. LATCH_EX
  14. Java的map键值对的用法,map的遍历,Entry对象的使用
  15. 【转】Android配置文件权限一览表
  16. [转载]如何解决failed to push some refs to git
  17. github push403错误的处理
  18. Tomcat设置欢迎页问题
  19. mysql允许数据库远程连接
  20. C# 序列化(Binary、Xml、Soap)

热门文章

  1. JsonSchema使用详解
  2. c#学习内容
  3. 复选框 ajax取得后台页面
  4. HIVE- 新建UDF范例
  5. 一款实现滑动切换效果的插件---swiper
  6. unity3D中一些有用的设置
  7. 将任意一个jQuery对象进行表单序列化,免除了提交请求时大量拼写表单数据的烦恼,支持键值对&lt;name&amp;value&gt;格式和JSON格式。
  8. Idea_学习_03_IDEA中使自定义类型的文件进行代码高亮识别
  9. codeforces 631A A. Interview
  10. 使用 Anthem.NET 的经验小结