poj 3729 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 n, m and k where n represents the length of S1, m 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 ≤ n, m ≤ 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的更多相关文章
- POJ3729 Facer’s string 后缀数组
Fa ...
- 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 ...
- POJ 2887:Big String(分块)
http://poj.org/problem?id=2887 题意:给出一个字符串,还有n个询问,第一种询问是给出一个位置p和字符c,要在位置p的前面插入c(如果p超过字符串长度,自动插在最后),第二 ...
- POJ 3336 Count the string (KMP+DP,好题)
参考连接: KMP+DP: http://www.cnblogs.com/yuelingzhi/archive/2011/08/03/2126346.html 另外给出一个没用dp做的:http:// ...
- POJ - 3541 - Given a string…
Given a string… Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 1819 Accepted: 390 C ...
- poj 2155 matrix 二维线段树 线段树套线段树
题意 一个$n*n$矩阵,初始全为0,每次翻转一个子矩阵,然后单点查找 题解 任意一种能维护二维平面的数据结构都可以 我这里写的是二维线段树,因为四分树的写法复杂度可能会退化,因此考虑用树套树实现二维 ...
- POJ 3376 Finding Palindromes EX-KMP+字典树
题意: 给你n个串串,每个串串可以选择和n个字符串拼接(可以自己和自己拼接),问有多少个拼接后的字符串是回文. 所有的串串长度不超过2e6: 题解: 这题由于是在POJ上,所以string也用不了,会 ...
- (转)ACM next_permutation函数
转自 stven_king的博客 这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>下面是以前的笔记 (1) int 类型的next_permuta ...
- next_permutation函数
这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>下面是以前的笔记 与之完全相反的函数还有prev_permutation (1) int 类 ...
随机推荐
- linuxC编程介绍
第一步:写完程序 /first.c/ #include <stdio.h> int main() { printf("hello,welcome to the LinuxC!\n ...
- iOS-UICollectionViewController 介绍
废话不多说,列几个列子 (几种情况下的做法): 情景一: 介绍:1. 在UIViewController 上加 UICollectionView (用代码 创建 UICollectionView). ...
- 用go run命令启动main package中的多个文件
linux: go run *.go windows: go build后运行main.exe
- python-1基础总结
输入 >>> name = input() 1--如果字符串里面有很多字符都需要转义,就需要加很多\,为了简化,Python还允许用r''表示''内部的字符串默认不转义,可以自己试 ...
- python 函数function
函数 当代码出现有规律的重复的时候,只写一次函数实现多次使用(调用) 可使用的函数: 自定义函数 内置函数:文档 https://docs.python.org/3/library/function ...
- 关于学习less后一些感悟
学习了一天的less发现,自己被自己弄的晕头转向,好在是学明白了! 一.写自己的第一个less css样式编程: 网址:http://www.1024i.com/demo/less/document. ...
- Android面试收集录8 HandlerThread详解
1.前言 我们知道在Android系统中,我们执行完耗时操作都要另外开启子线程来执行,执行完线程以后线程会自动销毁. 想象一下如果我们在项目中经常要执行耗时操作,如果经常要开启线程,接着又销毁线程, ...
- Go实现try-catch-finally机制
前言 许多主流语言诸如:Java.Python都实现了try-catch-finally机制,而Go处理错误的方式却与前两种语言不同.关于Go处理异常的方式是好是坏仁者见仁智者见智,笔者还是更喜欢tr ...
- 万年历Calendar、js修改日期
//万年历 Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE,-1); //改变日期,改变年份.月份类似 SimpleDateF ...
- VS Extension+NVelocity系列(二)——让VS支持 NVelocity的智能提示(上)
一.基础概念 应该庆幸的是,VS的插件是靠着MEF实现而不是MAF,这让你所做的工作减轻了许多.如果在这之前,您已经了解了MEF的原理,我想对于VS插件的编写,您应该是很容易就能理解的.看看几个VS2 ...