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. Contest Hunter 模拟赛09 A [线段树维护斜率]

    题面 传送门 思路 首先看看我们到底要干什么:有$1e6$次询问,遍历$i$,每次要求一个形如$b_i \ast a_j - a_i \ast b_j$的东西的最大值 考虑如果一个$j$的决策在当前的 ...

  2. Linux和Windows上实现的异同-Linux的自适应ACK

    上周有同事问,延迟ACK到底对应用层会产生什么后果,我也不知道该如何作答,于是丢了一个链接: TCP之Delay ACK在Linux和Windows上实现的异同-Linux的自适应ACK: 是的,这是 ...

  3. BZOJ3533 [Sdoi2014]向量集 【线段树 + 凸包 + 三分】

    题目链接 BZOJ3533 题解 我们设询问的向量为\((x_0,y_0)\),参与乘积的向量为\((x,y)\) 则有 \[ \begin{aligned} ans &= x_0x + y_ ...

  4. nowcoder 提高组模拟赛 选择题 解题报告

    选择题 链接: https://www.nowcoder.com/acm/contest/178/B 来源:牛客网 题目描述 有一道选择题,有 \(a,b,c,d\) 四个选项. 现在有 \(n\) ...

  5. React context基本用法

    React的context就是一个全局变量,可以从根组件跨级别在React的组件中传递.React context的API有两个版本,React16.x之前的是老版本的context,之后的是新版本的 ...

  6. 非阻塞式JavaScript脚本及延伸知识

    JavaScript 倾向于阻塞浏览器某些处理过程,如HTTP 请求和界面刷新,这是开发者面临的最显著的性能问题.保持JavaScript文件短小,并限制HTTP请求的数量,只是创建反应迅速的网页应用 ...

  7. video视频在结束之后回到初始状态

    目前尝试了两种解决方案,但是方案1在安卓移动端无法生效(猜测是因为移动端安卓启动的是原生的视频播放控件的原因) 方案一: 重新load资源,这种方法比较简洁,但是在安卓下不适用 video.addEv ...

  8. VR行业纷纷倒闭:有硬件没内容

    从去年年底开始,VR就成为了一个流行词汇,不仅是巨头公司砸钱布局,众多创业公司也纷纷投入其中.但是,一窝蜂拥入的企业基本都没有成熟的商业模式和赢利模式,只能靠融资供血.在资本寒冬中,大部分的VR企业开 ...

  9. 【BZOJ4766】文艺计算姬 [暴力]

    文艺计算姬 Time Limit: 1 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description "奋战三星期,造台计算机 ...

  10. Bzoj1692 洛谷P2870 [Usaco2007 Dec]队列变换

    Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1570  Solved: 656 Description FJ打算带他的N(1 <= N <= ...