1087: Common Substrings

Time Limit:3000/1000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java/Others)
Total Submissions:857   Accepted:112

[Submit][Status][Discuss]

Description

You are given two long strings A

and B. They are comprised of lowercase letters. You should compute how many suffixes of A are the prefixes of B

.

Input

In the first line is a number T

(0<T≤100

) , indicating the cases following.
In the next T lines each line contains two strings — A

and B

.
( 0<|A|≤105,0<|B|≤105
)

Output

There should be exactly T
lines.
Each line contain a number — the answer.

Sample Input

1
abcc ccba

Sample Output

2

HINT

In the first case, cc and c are two of the suffixes of string A, and they are the prefixes of string B.

【分析】H[i]-H[L]*xp[L]表示从s[i]开始的长度为L的字符串的哈希值。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
using namespace std;
typedef long long ll;
const ll N = 1e5+;
const int x=;
ll H1[N],H2[N],xp[N];
ll hs1,hs2;
int ans;
char stra[N],strb[N];
int main()
{
int T;
scanf("%d",&T);
while (T--){
scanf("%s%s",stra,strb);
int lena=strlen(stra),lenb=strlen(strb);
H1[lena]=,H2[lenb]=;
for (int i=lena-;i>=;--i)
H1[i]=H1[i+]*x+(stra[i]-'a');
for (int i=lenb-;i>=;--i){
H2[i]=H2[i+]*x+(strb[i]-'a');
}
xp[]=;
ans=;
for (int i=;i<=max(lena,lenb);++i)
xp[i]=xp[i-]*x;
for (int L=;L<=min(lena,lenb);++L){
hs2=H2[]-H2[L]*xp[L];
hs1=H1[lena-L]-H1[lena]*xp[L];
if (hs2==hs1)
ans++;
}
printf("%d\n",ans);
}
return ;
}

1087: Common Substrings (哈希)的更多相关文章

  1. poj 3415 Common Substrings(后缀数组+单调栈)

    http://poj.org/problem?id=3415 Common Substrings Time Limit: 5000MS   Memory Limit: 65536K Total Sub ...

  2. 【POJ3415】 Common Substrings(后缀数组|SAM)

    Common Substrings Description A substring of a string T is defined as: T(i, k)=TiTi+1...Ti+k-1, 1≤i≤ ...

  3. 字符串(后缀数组):POJ 3415 Common Substrings

    Common Substrings   Description A substring of a string T is defined as: T(i, k)=TiTi+1...Ti+k-1, 1≤ ...

  4. POJ3415 Common Substrings —— 后缀数组 + 单调栈 公共子串个数

    题目链接:https://vjudge.net/problem/POJ-3415 Common Substrings Time Limit: 5000MS   Memory Limit: 65536K ...

  5. Common Substrings POJ - 3415 (后缀自动机)

    Common Substrings \[ Time Limit: 5000 ms\quad Memory Limit: 65536 kB \] 题意 给出两个字符串,要求两个字符串公共子串长度不小于 ...

  6. POJ 3415 Common Substrings(后缀数组 + 单调栈)题解

    题意: 给两个串\(A.B\),问你长度\(>=k\)的有几对公共子串 思路: 先想一个朴素算法: 把\(B\)接在\(A\)后面,然后去跑后缀数组,得到\(height\)数组,那么直接\(r ...

  7. POJ 3415 Common Substrings ——后缀数组

    [题目分析] 判断有多少个长度不小于k的相同子串的数目. N^2显然是可以做到的. 其实可以维护一个关于height的单调栈,统计一下贡献,就可以了. 其实还是挺难写的OTZ. [代码] #inclu ...

  8. POJ 3415 Common Substrings 后缀数组+并查集

    后缀数组,看到网上很多题解都是单调栈,这里提供一个不是单调栈的做法, 首先将两个串 连接起来求height   求完之后按height值从大往小合并.  height值代表的是  sa[i]和sa[i ...

  9. 【POJ 3415】Common Substrings 长度不小于k的公共子串的个数

    长度不小于k的公共子串的个数,论文里有题解,卡了一上午,因为sum没开long long!!! 没开long long毁一生again--- 以后应该早看POJ里的Discuss啊QAQ #inclu ...

随机推荐

  1. [BZOJ1449] [JSOI2009]球队收益 / [BZOJ2895] 球队预算

    Description 在一个篮球联赛里,有n支球队,球队的支出是和他们的胜负场次有关系的,具体来说,第i支球队的赛季总支出是Cix^2+Diy^2,Di<=Ci.(赢得多,给球员的奖金就多嘛) ...

  2. [洛谷P4782]【模板】2-SAT 问题

    题目大意:有$n$个布尔变量 $x_1 \sim x_n$,另有$m$个需要满足的条件,每个条件的形式都是"$x_i$ 为$true/false$或$x_j$为$true/false$&qu ...

  3. 算法学习——kruskal重构树

    kruskal重构树是一个比较冷门的数据结构. 其实可以看做一种最小生成树的表现形式. 在普通的kruskal中,如果一条边连接了在2个不同集合中的点的话,我们将合并这2个点所在集合. 而在krusk ...

  4. [Leetcode] Longest consecutive sequence 最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  5. BZOJ1787 [Ahoi2008]Meet 紧急集合 【LCA】

    1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec  Memory Limit: 162 MB Submit: 3578  Solved: 1635 [Submi ...

  6. SCU3037 Painting the Balls

    Description Petya puts the \(N\) white balls in a line and now he wants to paint some of them in bla ...

  7. 理解[].forEach.call()

    例子: let cols = document.querySelectorAll('ul li') [].forEach.call(cols, function (col, index) { // T ...

  8. CentOS 6通过yum升级Git

    By francis_hao    Mar 9,2017   在一个新机器上推送代码到github上时出现了下面的问题 error: The requested URL returned error: ...

  9. Membership Service Providers (MSP)

    https://blog.csdn.net/baidu_39649815/article/details/76468249 Membership service provider (MSP)是一个提供 ...

  10. Could not resolve com.android.support:multidex:1.0.2

    http://blog.csdn.net/goodlixueyong/article/details/50992835