Facer’s string
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 2155   Accepted: 644

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 题意:有两个字符串s1,s2,现在要从s1中找到子串s3,其长度必须大于等于K,使得s3也是s2的子串,并且s3再加上s1中的一个后继字符后不再是s2的子串,问满足条件的s3的数量。
思路:求出后缀数组和高度数组,那么对于s1中每一个后缀,判断这个后缀和s2中的任意一个后缀的lcp是否等于K。可以换个角度考虑这个问题,在lcp数组中,把数值大于等于K的连续的几个后缀的lcp当成一块来考虑,
如果这个块中s1,s2中的后缀都有,说明这些后缀当中的任意两个后缀的lcp都是大于等于K的,这样把lcp数组分成一块一块来考虑就能很快求出A中所有后缀和B中后缀的lcp大于等于K的数量,正好等于K的数量就是
sum(K)-sum(K+1)
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N_MAX = + ;
typedef long long ll;
int n, N,M, k, K;
int Rank[N_MAX * ];
int tmp[N_MAX * ];
int sa[N_MAX * ];
int lcp[N_MAX * ];
int s[N_MAX * ];
bool compare_sa(const int& i, const int& j) {
if (Rank[i] != Rank[j])return Rank[i] < Rank[j];
else {
int ri = i + k <= n ? Rank[i + k] : -;
int rj = j + k <= n ? Rank[j + k] : -;
return ri < rj;
}
} void construct_sa(const int *S, int *sa) {
for (int i = ; i <= n; i++) {
sa[i] = i;
Rank[i] = i < n ? S[i] : -;
}
for (k = ; k <= n; k *= ) {
sort(sa, sa + n + , compare_sa);
tmp[sa[]] = ;
for (int i = ; i <= n; i++) {
tmp[sa[i]] = tmp[sa[i - ]] + (compare_sa(sa[i - ], sa[i]) ? : );
}
for (int i = ; i <= n; i++) {
Rank[i] = tmp[i];
}
}
}
void construct_lcp(const int *S, int *sa, int *lcp) {
memset(lcp, , sizeof(lcp));
for (int i = ; i <= n; i++)Rank[sa[i]] = i;
int h = ;
lcp[] = ;
for (int i = ; i < n; i++) {
int j = sa[Rank[i] - ];
if (h > )h--;
for (; j + h < n&&i + h < n; h++) {
if (S[j + h] != S[i + h])break;
}
lcp[Rank[i] - ] = h;
}
} ll find_num(int K) {
int A = , B = ;
ll res = ;
for (int i = ; i < n;i++) {
if (lcp[i] < K) {
if (B > )res += A;
A = ; B = ;
}
if (sa[i + ] < N)A++;
if (sa[i + ] > N) B++;
}
return res;
} int main() {
while (scanf("%d%d%d", &N, &M, &K) != EOF) {
for (int i = ; i < N; i++) {
scanf("%d", &s[i]);
s[i]++;
}
s[N] = '$';
for (int i = N + ; i < N + M + ; i++) {
scanf("%d", &s[i]);
s[i]++;
}
n = N + M + ;
s[n] = ;
construct_sa(s, sa);
construct_lcp(s, sa, lcp);
printf("%lld\n",find_num(K)-find_num(K+));
}
return ;
}

poj 3729 Facer’s string的更多相关文章

  1. POJ3729 Facer’s string 后缀数组

                                                                                                      Fa ...

  2. HDU 3260/POJ 3827 Facer is learning to swim(DP+搜索)(2009 Asia Ningbo Regional)

    Description Facer is addicted to a game called "Tidy is learning to swim". But he finds it ...

  3. POJ 2887:Big String(分块)

    http://poj.org/problem?id=2887 题意:给出一个字符串,还有n个询问,第一种询问是给出一个位置p和字符c,要在位置p的前面插入c(如果p超过字符串长度,自动插在最后),第二 ...

  4. POJ 3336 Count the string (KMP+DP,好题)

    参考连接: KMP+DP: http://www.cnblogs.com/yuelingzhi/archive/2011/08/03/2126346.html 另外给出一个没用dp做的:http:// ...

  5. POJ - 3541 - Given a string…

    Given a string… Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 1819   Accepted: 390 C ...

  6. poj 2155 matrix 二维线段树 线段树套线段树

    题意 一个$n*n$矩阵,初始全为0,每次翻转一个子矩阵,然后单点查找 题解 任意一种能维护二维平面的数据结构都可以 我这里写的是二维线段树,因为四分树的写法复杂度可能会退化,因此考虑用树套树实现二维 ...

  7. POJ 3376 Finding Palindromes EX-KMP+字典树

    题意: 给你n个串串,每个串串可以选择和n个字符串拼接(可以自己和自己拼接),问有多少个拼接后的字符串是回文. 所有的串串长度不超过2e6: 题解: 这题由于是在POJ上,所以string也用不了,会 ...

  8. (转)ACM next_permutation函数

    转自 stven_king的博客 这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>下面是以前的笔记  (1) int 类型的next_permuta ...

  9. next_permutation函数

    这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>下面是以前的笔记    与之完全相反的函数还有prev_permutation  (1) int 类 ...

随机推荐

  1. centos7重启apache、nginx、mysql、php-fpm命令

    apache启动systemctl start httpd停止systemctl stop httpd重启systemctl restart httpd mysql启动systemctl start ...

  2. lan口和wan口的配置

    路由器的一排网线接口,分为 lan 和 wan .但不是谁生来就是lan口 或者 wan口 . 也没有谁规定就一个wan口 就只有一个. 网口就是网口, 决定它是 lan口 还是 wan口 ,是由我们 ...

  3. 1、spring boot入门

    1.Spring Boot 简介 简化Spring应用开发的一个框架: 整个Spring技术栈的一个大整合: J2EE开发的一站式解决方案: 2.微服务 2014,martin fowler 微服务: ...

  4. Vsftpd服务 和 TFTP协议

    FTP 文件传输协议 (File Transfer Protocol) FTP是一种在互联网中进行文件传输的协议,基于客户端/服务器模式,默认使用20.21号端口,其中端口20(数据端口)用于进行数据 ...

  5. Hive 压缩技术Data Compression

    Mapreducwe 执行流程 :input > map > shuffle > reduce > output 压缩执行时间,map 之后,压缩,数据存储在本地磁盘,减少磁盘 ...

  6. Android Studio的Log日志调试

    本人菜鸟一枚,极大发挥了搜索的功能.现记录一番,以备后患. 用断点真的很烦,因为之前写linux的时候,就是用最蠢但是也是挺有帮助的printf()来进行调试. 其实用Log输出日志的原理也是差不多的 ...

  7. 20145202马超 《Java程序设计》第二周学习总结

    一.课后练习题目 1.D 并没有给number赋值导致出现错误. 2.A 10/3相当于10=3*3+1: 10/3=3. 3.D 4.D 很明显,byte类型的数据的话300就会出现溢出的情况. 5 ...

  8. CodeForces 873D Merge Sort 构造 分治

    题意 给出一个归并排序的算法\(mergesort\),如果对于当前区间\([l, r)\)是有序的,则函数直接返回. 否则会分别调用\(mergesort(l, mid)\)和\(mergesort ...

  9. day04_08-while查询所有行

    <?php $link = @mysql_connect('localhost','root',''); mysql_query('use test',$link); mysql_query(' ...

  10. Nova 如何统计 OpenStack 资源

    1.云计算的本质在于将硬件资源软件化,以达到快速按需交付的效果,最基本的计算.存储和网络基础元素并没有因此改变.就计算而言,CPU.RAM 和 DISK等依旧是必不可少的核心资源. 从源代码和数据库相 ...