题意:给定一个字符串,问你它有多少个子串恰好出现 k 次。

析:后缀数组,先把height 数组处理出来,然后每次取 k 个进行分析,假设取的是 i ~ i+k-1,那么就有重复的,一个是 i-1 ~ i+k-1,另一个是 i ~ i+k,但是这样就删多了,再加上 i - 1 ~ i+k,这样就OK了。

代码如下:

#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>
#include <list>
#include <assert.h>
#include <bitset>
#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 fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(x,n) for(int i = (x); i < (n); ++i)
#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 double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 50;
const int mod = 1000;
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 s[maxn], sa[maxn], t[maxn], t2[maxn];
int h[maxn], r[maxn], c[maxn];
int n;
int dp[maxn][20]; 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){
if(L == R) return L;
++L;
int k = int(log(R-L+1) / log(2.0));
return min(dp[L][k], dp[R-(1<<k)+1][k]);
}
};
char s[maxn];
Array arr; int main(){
int T; cin >> T;
while(T--){
int k;
arr.init();
scanf("%d", &k);
scanf("%s", s);
for(int i = 0; s[i]; ++i)
arr.s[arr.n++] = s[i] - 'a' + 1;
arr.s[arr.n++] = 0;
arr.build_sa(28);
arr.getHight();
arr.rmq_init();
int ans = 0;
for(int i = 1; i < arr.n; ++i){
int l = i+k-1 < arr.n ? arr.query(i, i+k-1) : 0;
int x = i-1 > 0 && i+k-1 < arr.n ? arr.query(i-1, i+k-1) :0;
int y = i+k < arr.n ? arr.query(i, i+k) : 0;
int z = i-1 > 0 && i+k < arr.n ? arr.query(i-1, i+k) : 0;
ans += l - x - y + z;
}
printf("%d\n", ans);
}
return 0;
}

  

最新文章

  1. MongoDB的CRUD操作
  2. iOS 自定义NavigationBar右侧按钮rightBarButtonItem--button
  3. 【Alpha阶段】第二次Scrum例会
  4. 一个叫 team 的表,里面只有一个字段name, 一共有4 条纪录,分别是a,b,c,d, 对应四个球队,现在四个球队进行比赛,用一条sql 语句显示所有可能的比赛组合.
  5. c#简单自定义异常处理日志辅助类
  6. Java中final、finally、finalize的区别
  7. maven打包源代码sources.jar和javadoc.jar帮助文档
  8. 优步北京B组(8月10日-8月16日奖励规则)
  9. java--jsp+ssh+select动态结合数据和选择(解)
  10. Eclipse用法和技巧二十二:快速调整字体大小
  11. 七牛整合 ueditor (拦住那头牛,七牛又如何)
  12. docker commit使用
  13. 我的第一个python web开发框架(31)——定制ORM(七)
  14. Java延时器
  15. Java tomcat Several ports (8005, 8080, 8009) required by Tomcat v9.0 Server at localhost
  16. springcolud文章收藏
  17. nginx 文档链接
  18. centos 支持复制与粘贴
  19. Turning off “Language Service Disabled” error message in VS2017
  20. ubuntu配置ftp server

热门文章

  1. HTML转义字符表
  2. 【学习笔记】Manacher
  3. 【UVa】12118 Inspector&#39;s Dilemma(欧拉道路)
  4. Java int Integer
  5. 指向“**js/shop.js”的 &lt;script&gt; 加载失败
  6. C#获取外网IP、本机MAC地址及Ping的实现
  7. C# 在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke [问题点数:40分
  8. sql server 2005 修改动态端口,连接字符串为:需要改成:IP地址+逗号+端口号才行
  9. MyBatis 学习记录3 MapperMethod类
  10. 解决MySql 数据库 提示:1045 access denied for user &#39;root&#39;@&#39;localhost&#39; using password yes