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 ...
随机推荐
- 设计模式--迪米特法则(Lod/LKP)
迪米特法则:(Law of Demeter, LoD),也称最少知识原则(Least Knowledge Principle, LKP) 理解: 假设两个类不必彼此直接通信,那么这两个类就不 ...
- $.each 和$(selector).each()的差别
Home » jQuery » $.each() $.each() Posted on 2012 年 3 月 15 日 in jQuery, jQuery函数 | by Jason | 译自官方手冊: ...
- 几个常用的ps命令
1. ps aux If you are looking for a short summary of the active processes, use ps aux [root@rhel7 tm ...
- noip 2005 等价表达式
/* 开始想的是 维护a的每个指数的系数 然而不好办 然而还有^10^10^10这种数据 特殊值带入吧 多搞几个素数 接下来就是玄学的事了 给a赋值之后 就是简单地表达式求值 虽然思路简单 但是字符串 ...
- 用timer控件实现sleep效果
有时候我们需要代码延迟执行,这就需要用到Thread.Sleep()这个方法,但这个方法在主线程使用时会造成界面假死.使用timer控件既能达到代码延迟执行的效果,又不会有假死的困扰. 假设我们需要在 ...
- 利用反射的特性将DataReader对象转化为List集合
问题:将SqlDataReader对象转换为List<T>集合 思路: 1,利用反射的特性得到对应实体Model的公共属性 Type type = typeof(T); PropertyI ...
- mySQL优化 my.ini 配置说明
[mysqld] port = 3306 serverid = 1 socket = /tmp/mysql.sock skip-name-resolve #禁止MySQL对外部连接进行DNS解析,使用 ...
- 关于iOS应用管理之九宫格的坐标计算以及与UIScrollView的结合
关于九宫格的布局以及坐标的计算,对于大多数的iOS初学者甚至有一定能力的学者来说都是一大难题,在此写者通过自己的开发经验以及多次应用,把自己的所学所得分享给大家,就通过应用管理来进行浅谈一二. ...
- iOS 天气应用代码中文介绍
天气应用 解释请求参数 q: 表示Location(可以给出城市名字;或者直接给城市的经纬度) 例子:q=beijing 例子 q=48.834,2.394 num_of_days: 需要预报的天数 ...
- 武汉科技大学ACM:1005: 华科版C语言程序设计教程(第二版)例题5.8
Problem Description 老师给小豪出了一道题目:给你两个整数x和n(-10<=x<=10,1<=n<=10),让你求出x^1+x^2+x^3+……+x^n的结果 ...