1087: Common Substrings (哈希)
1087: Common Substrings
Total Submissions:857 Accepted:112
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
(0<T≤100
and B
Output
Sample Input
1
abcc ccba
Sample Output
2
HINT
【分析】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 (哈希)的更多相关文章
- poj 3415 Common Substrings(后缀数组+单调栈)
http://poj.org/problem?id=3415 Common Substrings Time Limit: 5000MS Memory Limit: 65536K Total Sub ...
- 【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≤ ...
- 字符串(后缀数组):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≤ ...
- POJ3415 Common Substrings —— 后缀数组 + 单调栈 公共子串个数
题目链接:https://vjudge.net/problem/POJ-3415 Common Substrings Time Limit: 5000MS Memory Limit: 65536K ...
- Common Substrings POJ - 3415 (后缀自动机)
Common Substrings \[ Time Limit: 5000 ms\quad Memory Limit: 65536 kB \] 题意 给出两个字符串,要求两个字符串公共子串长度不小于 ...
- POJ 3415 Common Substrings(后缀数组 + 单调栈)题解
题意: 给两个串\(A.B\),问你长度\(>=k\)的有几对公共子串 思路: 先想一个朴素算法: 把\(B\)接在\(A\)后面,然后去跑后缀数组,得到\(height\)数组,那么直接\(r ...
- POJ 3415 Common Substrings ——后缀数组
[题目分析] 判断有多少个长度不小于k的相同子串的数目. N^2显然是可以做到的. 其实可以维护一个关于height的单调栈,统计一下贡献,就可以了. 其实还是挺难写的OTZ. [代码] #inclu ...
- POJ 3415 Common Substrings 后缀数组+并查集
后缀数组,看到网上很多题解都是单调栈,这里提供一个不是单调栈的做法, 首先将两个串 连接起来求height 求完之后按height值从大往小合并. height值代表的是 sa[i]和sa[i ...
- 【POJ 3415】Common Substrings 长度不小于k的公共子串的个数
长度不小于k的公共子串的个数,论文里有题解,卡了一上午,因为sum没开long long!!! 没开long long毁一生again--- 以后应该早看POJ里的Discuss啊QAQ #inclu ...
随机推荐
- shell脚本应用
解析乱的日志文件到临时文件中,然后用awk 1004 cd /usr/local 1005 ll 1006 cd pttmsg/ 1007 ll 1008 cd msgbin-2/ ...
- Win10的WSL很好用呀
WSL全名是Windows Subsystem for Linux,是win10版本号16xx之后推出的开发者功能,提供了如原生linux版的体验. 最近最新的win10春季版1803出来了,安装了看 ...
- HDU 2844 二进制优化的多重背包
Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- java的哈希遍历 hashmap
Map<String,String> map = new HashMap<String, String>(); map.put("title"," ...
- Java多线程-一个简单的线程,实现挂起和恢复的功能
public class MySprite implements Runnable { /* * 线程用变量 */ private boolean running = false; private b ...
- c++(类) this指针
this指针的相关概念: this只能在成员函数中使用.全局函数,静态函数都不能使用this.实际上,成员函数默认第一个参数为T* const register this. 为什么this指针不能再静 ...
- c#之字符串函数
1.常用的字符串函数 Compare 比较字符串的内容,考虑文化背景(场所),确定某些字符是否相等 int Compare(string str1,string str2) int Compare(s ...
- 知问前端——工具提示UI
工具提示(tooltip),是一个非常实用的UI.它彻底扩展了HTML中的title属性,让提示更加丰富,更加可控制,全面提升了用户体验. 调用tooltip()方法 在调用tooltip()方法之前 ...
- 【NOIP1999】邮票面值设计 dfs+dp
题目传送门 这道题其实就是找一波上界比较麻烦 用一波 背包可以推出上界mx 所以新加入的物品价值一旦大于mx+1,显然就会出现断层,所以可以以maxm+1为枚举上界,然后这样进行下一层的dfs. 这样 ...
- CCCC练习即感
字符串进行初始化时不能通过char a[10]={'\0'}来简单进行,写循环或者memset,亲测有效,以及初始化分好情况,用空格还是'\0',别乱搞. 有一个有意思的题,连续因子,从2开始,依次向 ...