当前位置:网站首页>总结20220209
总结20220209
2022-07-22 09:08:00 【蒟蒻的菜鸡】
上午仔细看了一下kmp算法,模板基本会用了,但是原理还要再想想,下午听课后加强了一些理解,然后刷了几个题,感觉有些没有用到hash算法什么的,c++可以解决很多的问题。
KMP字符串匹配
题目描述
给出两个字符串 s_1s1 和 s_2s2,若 s_1s1 的区间 [l, r][l,r] 子串与 s_2s2 完全相同,则称 s_2s2 在 s_1s1 中出现了,其出现位置为 ll。
现在请你求出 s_2s2 在 s_1s1 中所有出现的位置。
定义一个字符串 ss 的 border 为 ss 的一个非 ss 本身的子串 tt,满足 tt 既是 ss 的前缀,又是 ss 的后缀。
对于 s_2s2,你还需要求出对于其每个前缀 s's′ 的最长 border t't′ 的长度。
输入格式
第一行为一个字符串,即为 s_1s1。
第二行为一个字符串,即为 s_2s2。
输出格式
首先输出若干行,每行一个整数,按从小到大的顺序输出 s_2s2 在 s_1s1 中出现的位置。
最后一行输出 |s_2|∣s2∣ 个整数,第 ii 个整数表示 s_2s2 的长度为 ii 的前缀的最长 border 长度。
输入输出样例
输入 #1复制
ABABABC ABA
输出 #1复制
1 3 0 0 1
#include<bits/stdc++.h>
using namespace std;
const int N=1000010;
int t[N];
char s[N],p[N];
int len1,len2;
void next() //求next数组
{
int j,i;
j=-1;
t[0]=-1;
i=0;
while(i<len2)
{
if(j==-1||p[i]==p[j])
{
i++;
j++;
t[i]=j;
}
else
j=t[j];
}
}
void kmp()
{
int i,j;
i=0;
j=0;
while(i<len1)
{
if(j==-1||s[i]==p[j]){
j++;
i++;
}
else //当不匹配时,开始考虑next数组
{
j=t[j];
}
if(j==len2){
cout<<i-len2+1<<endl; //输出符合要求的位置
j=t[j];
}
}
}
int main()
{
int i,j;
cin>>s;
len1=strlen(s);
cin>>p;
len2=strlen(p);
next();
kmp();
for(int i=1;i<=len2;i++)cout<<t[i]<<' ';
return 0;
}
Barn Echoes G
题目描述
The cows enjoy mooing at the barn because their moos echo back, although sometimes not completely. Bessie, ever the excellent
secretary, has been recording the exact wording of the moo as it goes out and returns. She is curious as to just how much overlap there is.
Given two lines of input (letters from the set a..z, total length in the range 1..80), each of which has the wording of a moo on it, determine the greatest number of characters of overlap between one string and the other. A string is an overlap between two other strings if it is a prefix of one string and a suffix of the other string.
By way of example, consider two moos:
moyooyoxyzooo
yzoooqyasdfljkamo
The last part of the first string overlaps 'yzooo' with the first part of the second string. The last part of the second string
overlaps 'mo' with the first part of the first string. The largest overlap is 'yzooo' whose length is 5.
POINTS: 50
奶牛们非常享受在牛栏中哞叫,因为她们可以听到她们哞声的回音。虽然有时候并不能完全听到完整的回音。Bessie曾经是一个出色的秘书,所以她精确地纪录了所有的哞叫声及其回声。她很好奇到底两个声音的重复部份有多长。
输入两个字符串(长度为1到80个字母),表示两个哞叫声。你要确定最长的重复部份的长度。两个字符串的重复部份指的是同时是一个字符串的前缀和另一个字符串的后缀的字符串。
我们通过一个例子来理解题目。考虑下面的两个哞声:
moyooyoxyzooo
yzoooqyasdfljkamo
第一个串的最后的部份"yzooo"跟第二个串的第一部份重复。第二个串的最后的部份"mo"跟第一个串的第一部份重复。所以"yzooo"跟"mo"都是这2个串的重复部份。其中,"yzooo"比较长,所以最长的重复部份的长度就是5。
输入格式
* Lines 1..2: Each line has the text of a moo or its echo
输出格式
* Line 1: A single line with a single integer that is the length of the longest overlap between the front of one string and end of the other.
输入输出样例
输入 #1复制
abcxxxxabcxabcd abcdxabcxxxxabcx
输出 #1复制
11
#include<bits/stdc++.h>
using namespace std;
const int N=1000010;
int t[N];
char s[N],p[N],s1[N],p1[N];
int len1,len2;
void next(char p[]) //求next数组
{
int j,i;
j=-1;
t[0]=-1;
i=0;
while(i<len2)
{
if(j==-1||p[i]==p[j])
{
i++;
j++;
t[i]=j;
}
else
j=t[j];
}
}
int kmp(char s[],char p[])
{
int i,j;
i=max(0,len1-len2);
j=0;
next(p);
while(i<len1)
{
if(j==-1||s[i]==p[j]){
j++;
i++;
}
else //当不匹配时,开始考虑next数组
{
j=t[j];
}
}
return j; //匹配的位置
}
int lh(char s[],char p[]) //联合两个字符串
{
int i,j;
len1=strlen(s);
len2=strlen(p);
int c=kmp(s,p);
i=len1-c;
for(j=0;j<len2;j++)
{
s[i++]=p[j]; //将新字符串加在长字符串后面
}
s[i]='\0';
return len1+len2-strlen(s);
}
int main()
{
cin>>s1;
cin>>p1;
cout<<max(lh(s1,p1),lh(p1,s1)); //取最大值
return 0;
}
边栏推荐
猜你喜欢
别让恐婚,扼杀你幸福!
【SDIO】SD2.0协议分析总结(三)-- SD卡相关命令介绍
Using pypyodbc in Ubuntu cannot connect to SQL Server
TCP与UDP及三次握手,四次挥手
creating vlan over openstack (by quqi99)
【STM32】STM32 SDIO SD卡读写测试(一)-- SD卡硬件设计和软件移植
Thread learning notes
24 SaaS thoughts
【STM32】STM32 SDIO SD卡读写测试(二)-- SD_Init之Power On阶段
PHP开发中csrf攻击的简单演示和防范
随机推荐
Tiktok massive engine 1 creates an advertising plan
Android互联网大厂面试经验
this指向问题
Copy of file descriptor
【SDIO】SDIO、SD卡、FatFs文件系统相关文章索引
批量查分爬虫
centos7.5下添加gd库然后mysql拓展库没了mysql拓展的配置也没问题,phpinfo中就是没有mysql拓展
阿里大于之前的短信服务用不了了
为什么chrome视频时卡得厉害(by quqi99)
ecshop提示 “模板文件 themesmobile/mo_paleng_moban/index.dwt 无法修改”?
2022-07-21: given a string STR and a positive number k, you can divide STR into multiple substrings at will, in order to find that in a certain division scheme, there are as many palindrome substrings
小程序实现列表和详情页
ecshop怎么在本地跑超级慢?
C语言简易TCP服务端程序
call()和apply()
set up ovn based sr-iov test env (by quqi99)
Reflection + annotation + generics
Toss Phoenix system (by quqi99)
Js高级-基本数据类型与引用数据类型
真的有必要定义VO,BO,PO,DO,DTO吗?