POJ3729 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 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
大致意思: 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 ;
}
POJ3729 Facer’s string 后缀数组的更多相关文章
- hdu 3553 Just a String (后缀数组)
hdu 3553 Just a String (后缀数组) 题意:很简单,问一个字符串的第k大的子串是谁. 解题思路:后缀数组.先预处理一遍,把能算的都算出来.将后缀按sa排序,假如我们知道答案在那个 ...
- hdu 6194 沈阳网络赛--string string string(后缀数组)
题目链接 Problem Description Uncle Mao is a wonderful ACMER. One day he met an easy problem, but Uncle M ...
- HDU 6194 string string string (后缀数组)
题意:给定一个字符串,问你它有多少个子串恰好出现 k 次. 析:后缀数组,先把height 数组处理出来,然后每次取 k 个进行分析,假设取的是 i ~ i+k-1,那么就有重复的,一个是 i-1 ~ ...
- Hackerrank--Ashton and String(后缀数组)
题目链接 Ashton appeared for a job interview and is asked the following question. Arrange all the distin ...
- hdu 5030 Rabbit's String(后缀数组&二分法)
Rabbit's String Time Limit: 40000/20000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- hdu-6194 string string string 后缀数组 出现恰好K次的串的数量
最少出现K次我们可以用Height数组的lcp来得出,而恰好出现K次,我们只要除去最少出现K+1次的lcp即可. #include <cstdio> #include <cstrin ...
- [Codechef CHSTR] Chef and String - 后缀数组
[Codechef CHSTR] Chef and String Description 每次询问 \(S\) 的子串中,选出 \(k\) 个相同子串的方案有多少种. Solution 本题要求不是很 ...
- HDU5008 Boring String Problem(后缀数组 + 二分 + 线段树)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5008 Description In this problem, you are given ...
- HDU5853 Jong Hyok and String(二分 + 后缀数组)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5853 Description Jong Hyok loves strings. One da ...
随机推荐
- ceph主要数据结构解析3-Ceph_fs.h文件
(1)集群内部子版本协议类型宏定义:与公共协议保持独立性,以便消息类型和协议升级受影响 #define CEPH_OSDC_PROTOCOL 24 /* server/client */OSD服务 ...
- myeclipse 于 否update software 解
In some situations you may not be able to install or update software using the menu commands in the ...
- unity3d优化IOS
1. using UnityEngine; class GarbageCollectManager : MonoBehaviour { public int frameFreq = 30; ...
- ViewPager 详解(三)---PagerTabStrip与PagerTitleStrip添加标题栏的异同
前言:在前两篇文章中,我们讲解了滑动页面的的实现方法与四大函数的意义,但有时,仅仅实现页面滑动是不够的,还要有标题栏才会显得更友好.所以在这篇文章中,我将会向大家展示在Android.support. ...
- 2015 UESTC Winter Training #6【Regionals 2010 >> North America - Rocky Mountain】
2015 UESTC Winter Training #6 Regionals 2010 >> North America - Rocky Mountain A - Parenthesis ...
- hdu 5108
//题意是给一个数N,然后让你求M,使得N/M为素数,并且M的值最小 //思路呢,大概有两种,一个是遍历素数求解的,不过数据太大不现实 //另外一种就是质因数求解,for循环是遍历质因数,然后whil ...
- 排序算法之快速排序 JAVA快速排序算法
public static void quickSort(int[] arr, int low , int height){ int l=low, h = height; if(low < he ...
- 匹配不含有某个信息的sql语句写法
SELECT id,order_id,flight_info FROM order_flights WHERE mark=0 AND flight_info REGEXP '[^() DAY)]' O ...
- DIV布局之道二:DIV块的嵌套,DIV盒子模型
本文讲解DIV块布局的第二种使用方式:嵌套.“DIV嵌套”在有些文献中也被称为“盒子模型”,说的通俗一点就是嵌套(一个大的DIV块内部又包含一个或多个DIV块). 请看如下代码: CSS部分: CSS ...
- Calling a C++ dll with unsigned char* parameters
unsigned char* 等价 BYTE* 例1: C++: int __stdcall LIVESCAN_GetFPRawData(int nChannel, unsigned char *p ...