首页 技术 正文
技术 2022年11月17日
0 收藏 998 点赞 2,480 浏览 3773 个字

                                                                                                  Facer’s string

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 1783   Accepted: 537

Description

Minifacer was very happy these days because he has learned the algorithm of KMP recently. Yet his elder brother, Hugefacer, thought that Minifacer needs a deeper understanding of this algorithm. Thus Hugefacer decided to play a game with his little brother to enhance his skills.

First, Hugefacer wrote down two strings S1 and S2. Then Minifacer tried to find a substring S3 of S1 which meets the following requirements: 1) S3 should have a length of k (which is a constant value); 2)S3 should also be the substring of S2. After several rounds, Hugefacer found that this game was too easy for his clever little brother, so he added another requirement: 3) the extended string of S3 should NOT be the substring of S2. Here the extended string of S3 is defined as S3 plus its succeed character in S1 (if S3 does not have a succeed character in S1, the extended string of S3 is S3 + ‘ ‘ which will never appear in S2). For example, let S1 be “ababc”, if we select the substring from the first character to the second character as S3 (so S3 equals “ab”), its extended string should be “aba”; if we select the substring from the third character to the fourth character as S3, its extended string should be “abc”; if we select the substring from the fourth character to the fifth character as S3, its extended string should be “bc”.

Since the difficult level of the game has been greatly increased after the third requirement was added, Minifacer was not able to win the game and he thought that maybe none of the substring would meet all the requirements. In order to prove that Minifacer was wrong, Hugefacer would like to write a program to compute number of substrings that meet the three demands (Note that two strings with same appearance but different positions in original string S1 should be count twice). Since Hugefacer do not like characters, he will use non-negative integers (range from 0 to 10000) instead.

Input

There are multiple test cases. Each case contains three lines: the first line contains three integers nm and k where n represents the length of S1m represents the length of S2 and k represents the length of substring; the second line contains string S1 and the third line contains string S2. Here 0 ≤ nm ≤ 50000. Input ends with EOF.

Output

For each test case, output a number in a line stand for the total number of substrings that meet the three requirements.

Sample Input

5 5 2
1 2 1 2 3
1 2 3 4 5
5 5 3
1 2 1 2 3
1 2 3 4 5

Sample Output

2
1

大致意思: a串所有后缀中,有多少个后缀与b串的所有后缀的lcp的最大值==k。。

a b串先连接一下,,中间加一个不会出现的数值,,我选了inf。。

然后求sa,lcp数组。。

可以先求大于等于k的后缀个数 ,再减去大于等于k+1的个数就是答案了。。

 #include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps = 1e-;
const int maxn = 5e4+;
int s[maxn<<];
int len, k, sa[maxn << ], tmp[maxn << ], rank[maxn << ];
bool cmp(int i, int j)
{
if (rank[i] != rank[j])
return rank[i] < rank[j];
else
{
int x = (i + k <= len ? rank[i+k] : -);
int y = (j + k <= len ? rank[j+k] : -);
return x < y;
}
}
void build_sa()
{
for (int i = ; i <= len; i++)
{
sa[i] = i;
rank[i] = (i < len ? s[i] : -);
}
for (k = ; k <= len; k *= )
{
sort(sa, sa+len+,cmp);
tmp[sa[]] = ;
for (int i = ; i <= len; i++)
{
tmp[sa[i]] = tmp[sa[i-]] + (cmp(sa[i-], sa[i]) ? : );
}
for (int i = ; i <= len; i++)
rank[i] = tmp[i];
}
}
int lcp[maxn << ];
void get_lcp()
{
for (int i = ; i <= len; i++)
{
rank[sa[i]] = i;
}
int h = ;
lcp[] = ;
for (int i = ; i < len; i++)
{
int j = sa[rank[i]-];
if (h > )
h--;
for (; j+h < len && i+h < len; h++)
if (s[j+h] != s[i+h])
break;
lcp[rank[i]] = h;
}
}
int solve(int kk, int n)
{
int res = ;
for (int i = ; i <= len; i++)
{
if (lcp[i] >= kk)
{
int one = , two = ;
if (sa[i-] < n)
one++;
if (sa[i-] > n)
two++;
for (; i < len && lcp[i] >= kk; i++)
{
if (sa[i] < n)
one++;
if (sa[i] > n)
two++;
}
if (two)
res += one;
}
}
return res;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int n,m,kk;
while (~ scanf ("%d%d%d",&n,&m,&kk))
{
for (int i = ; i < n; i++)
{
scanf ("%d", s+i);
s[i]++;
}
s[n] = inf;
for (int i = n+; i < n++m; i++)
{
scanf ("%d", s+i);
s[i]++;
}
len = n + m + ;
build_sa();
get_lcp();
printf("%d\n",solve(kk,n) - solve(kk+,n));
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,076
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,812
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894