当前位置:网站首页>输入两个字符串,输出最长相同子串
输入两个字符串,输出最长相同子串
2022-06-22 17:50:00 【Douglas_LT】
#pragma warning(disable :4996)
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include <stdio.h>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
typedef long long LL;
const LL MOD = 1000000007;
const LL P = 1000009;
const LL MAXN = 1010;
LL powP[MAXN] = {
0 }, H1[MAXN] = {
0 }, H2[MAXN] = {
0 };
vector<pair<int, pair<int, int>>> pr1, pr2;
void init(int len)
{
powP[0] = 1;
for (int i = 1; i < len; i++)
{
powP[i] = (powP[i - 1] * P) % MOD;
}
}
void calH(LL H[],string str)
{
H[0] = str[0];
for (int i = 0; i < str.length(); i++)
{
H[i] = (H[i - 1] * P + str[i]) % MOD;
}
}
LL calSingleSubH(LL H[],int i,int j)
{
if (i == 0)
{
return H[j];
}
else
{
return abs(H[j] - H[i - 1] * powP[j - i + 1]) % MOD;
}
}
void calSubH(LL H[], int len,vector<pair<int, pair<int, int>>>& pr)
{
for (int i = 0; i < len; i++)
{
for (int j = 1; j < len; j++)
{
int hashvalue = calSingleSubH(H, i, j);
pr.push_back(make_pair(hashvalue,make_pair(i,j)));
}
}
}
int beginn=0, endd=0;
void getmax()
{
pair<int, int>p;
int ans = 0;
for (int i = 0; i < pr1.size(); i++)
{
for (int j = 0; j < pr2.size(); j++)
{
if (pr1[i].first == pr2[j].first)
{
p=pr1[i].second;
int temp1 = p.first,temp2=p.second;
if (temp2 - temp1 > ans)
{
ans = temp2 - temp1;
beginn = temp1;
endd = temp2;
}
}
}
}
}
int main()
{
string s1, s2;
cin >> s1 >> s2;
int len1 = s1.length(), len2 = s2.length();
init(max(len1, len2));
calH(H1, s1);
calH(H2, s2);
calSubH(H1, len1, pr1);
calSubH(H2, len2, pr2);
getmax();
for (int i = beginn; i <= endd; i++)
{
cout << s1[i];
}
return 0;
}
边栏推荐
- AUTOCAD——五种标注快捷键
- 使能伙伴,春节重大保障“不停歇”
- Interview MySQL
- Golang实现基于Redis的可靠延迟队列
- 传统图像--LBP特征
- In May, 2022, China's game manufacturers and applications went to sea, with top 30 revenue in EMEA region
- STM32控制矩阵按键,HAL库,cubeMX配置
- Cookie加密3+RPC解法
- Zynq UltraScale + RFSoC ZCU111 RF时钟树学习 1
- Jenkins configuration project integration pin notification
猜你喜欢

面试MySQL

5G 短消息解决方案

牛客网:判断是否为回文字符串
Set of redis data structure

如何持续突破性能表现? | DX研发模式

Redis中的布隆过滤器与布谷鸟过滤器,你了解多少?

巴比特 | 元宇宙每日必读:传腾讯成立XR部门,元宇宙板块再次上涨,多家券商发报告关注虚拟人的投资机会...

2022年5月中国游戏厂商及应用出海 EMEA 地区收入30强

A course for New Oriental transformation bilingual live broadcast to bring goods to the project manager

2022 G2 power plant boiler stoker question bank and online simulation examination
随机推荐
函数的导数与微分的关系
2022 R2 mobile pressure vessel filling test question simulation test platform operation
wpa_ State machine migration of supplicant
Interview MySQL
jniLibs.srcDirs = [‘libs‘]有什么用?
2022 operation of simulated examination platform for examination question bank of welder (elementary) special operation certificate
阿里云过户找不到账号安全组ID问题
2022 G2 power plant boiler stoker question bank and online simulation examination
How much do you know about the bloom filter and cuckoo filter in redis?
@Lucky user of "Qilu Duojiao", Shandong 5A scenic spot calls you to visit the park for free!
At 19:30 today, the science popularization leader said that he would take you to explore how AI can stimulate human creativity
IPLOOK 5GC与亚信国际CHF(计费功能)对接成功
Complete the sqlsession interface and implementation classes
In May, 2022, China's game manufacturers and applications went to sea, with top 30 revenue in EMEA region
RSPS2022 Finalist | Dr. Yang Bai 简介
数组模拟栈
助力客户数字化转型,构建全新的运维体系
sqlserver保存时遇到这个页面怎么回事啊
Some preliminary explorations of avoiding obstacles and finding paths by rays in unity
Exness sorted out three problems to be solved in Musk's acquisition of Twitter