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. while循环,格式化输出,运算符

      1.while循环 1.while 基本机构: while 条件: 循环体 执行流程: 当条件成立时为真,执行循环体. 再次判断条件是否成立,如果成立再次执行. 当判断条件结果为假时,跳出循环,本 ...

  2. 彻底搞定C指针--“函数名与函数指针”

    函数名与函数指针   一 通常的函数调用 一个通常的函数调用的例子: //自行包含头文件 void MyFun(int x); //此处的申明也可写成:void MyFun( int ); 点击打开链 ...

  3. Python__关于列表的引用 以append操作为例

    对于列表这样的可变类型来说,对它操作是不会改变内存地址的. 若列表里面存的元素是整数这样的不可变类型,若修改这个元素那地址还是会改变,如: >>> a = [,,] >> ...

  4. 简单的for循环实现九九乘法表

    PHP for 循环 语法 for (init counter; test counter; increment counter) { code to be executed; } 参数: init ...

  5. Laravel5.5.x集成Swagger (L5-Swagger) 只讲Laravel5.5.x的集成,laravel其他版本请自行研究或参考github上的说明

    --------上图 截取自Github 官网上的安装参考----------------------------------------------------------------------- ...

  6. kivy学习二:做一个查询所在地区身份证前6位的小软件

    经过半个月的尝试,终于成功,记录下来备查! 做完之后发现有很多的问题没有解决,请大佬多批评指教! 强烈建议:学习KIVY的查看官方文档 需要用的知识: 1.字典的相关知识 2.kivy的下拉列表(Dr ...

  7. HNU暑假训练第一场C.Ninja Map

    一.题目大意 Intersections of Crossing Path City are aligned to a grid. There are N east-west streets whic ...

  8. PHP.28-TP框架商城应用实例-后台5-多表操作-商品表与品牌表

    表与表之间的关系:1:1 1:多 多:多 功能需求决定表关系 此处的表关系为:品牌表:商品表=1:多 1.首先在表结构上关联,在多的表(商品表)添加一个字段,关联一的表(品牌表)的ID(主键) 添加字 ...

  9. CSS继承特殊

    继承 CSS的某些样式具有继承性.继承是一种规则,它允许样式不仅作用于某个特定html标签元素,而且应用于其后代   如:在p中的所有字体都为红色     p{color:red;}    <p ...

  10. 《Cracking the Coding Interview》——第9章:递归和动态规划——题目4

    2014-03-20 03:08 题目:给定一个集合,返回其幂集. 解法:DFS. 代码: // 9.4 Return all subsets of a set #include <cstdio ...