[USACO09OCT]谷仓里的回声Barn Echoes(hush、STL)
https://www.luogu.org/problem/P2957
题目描述
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
输出
说明
'abcxxxxabcx' is a prefix of the first string and a suffix of the second string.
题目大意
给两个字符串,找出最大重复长度(两个字符串的重复部份指的是同时是一个字符串的前缀和另一个字符串的后缀的字符串)
暴力解法:
- #include <stdio.h>
- #include <string.h>
- #include <iostream>
- #include <string>
- #include <math.h>
- #include <algorithm>
- #include <vector>
- #include <queue>
- #include <set>
- #include <stack>
- #include <map>
- #include <math.h>
- const int INF=0x3f3f3f3f;
- typedef long long LL;
- const int mod=1e9+;
- const int maxn=1e5+;
- using namespace std;
- //ios::sync_with_stdio(false);
- // cin.tie(NULL);
- int main()
- {
- string s1,s2;
- cin>>s1>>s2;
- int len1=s1.size();
- int len2=s2.size();
- string ss="";
- int ans=;
- for(int i=;i<=len1;i++)
- {
- ss+=s1[i];
- if(i+>len2)
- break;
- if(s2.find(ss,len2-(i+))!=string::npos)
- ans=i+;
- }
- ss="";
- for(int i=;i<=len2;i++)
- {
- ss+=s2[i];
- if(i+>len1)
- break;
- if(s1.find(ss,len1-(i+))!=string::npos)
- ans=max(ans,i+);
- }
- cout<<ans<<endl;
- }
思路一样,但更加简洁的写法
- #include <stdio.h>
- #include <string.h>
- #include <iostream>
- #include <string>
- #include <math.h>
- #include <algorithm>
- #include <vector>
- #include <queue>
- #include <set>
- #include <stack>
- #include <map>
- #include <math.h>
- const int INF=0x3f3f3f3f;
- typedef long long LL;
- const int mod=1e9+;
- const int maxn=1e5+;
- using namespace std;
- //ios::sync_with_stdio(false);
- // cin.tie(NULL);
- int main()
- {
- string s1,s2;
- cin>>s1>>s2;
- int ans=;
- for(int i=;i<=min(s1.size(),s2.size());i++)//比较s1与s2长度,并取最小
- {
- if(s1.substr(,i)==s2.substr(s2.size()-i,i))//提取子串
- ans=max(ans,i);
- if(s2.substr(,i)==s1.substr(s1.size()-i,i))
- ans=max(ans,i);
- }
- cout<<ans<<endl;
- }
下面是别人的hush做法:
分析
比如两个字符串a、b,因为有可能是a的头和b的尾、b的头和a的尾两种情况,那么我们就处理出a的头、a的尾、b的头、b的尾,然后再for一遍找最大值即可。
对于这两个字符串的头和尾,我们就可以用到字符串hash,front[i]、back[i]分别表示:前i个字符的hash值和后i个字符的hash值,其实就是一个类似于前缀和和后缀和的东西(前缀后缀和与本题无关)。
最后我们再取fronta和backb、frontb、backa分别扫一遍,找出hash值相同的最大位置,就是最长的重复部分。
代码
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #define ll long long
- #define mod 192**817//咳咳
- #define ins 11111
- using namespace std;
- char a[];//字符串a
- char b[];//字符串b
- int lena,lenb;//两个字符串的长度
- int fronta[],backa[];//a的前缀、后缀hash值
- int frontb[],backb[];//b的前缀、后缀hash值
- int ans;//记录覆盖长度
- int maxx;//记录最大覆盖长度
- int front_insert_hash(char s[],int len)//计算前缀hash值
- {
- ll sum=;//初始化hash值
- for(int i=;i<len;i++)
- sum=(sum*ins+(ll)s[i])%mod;//计算hash值
- return sum;//返回hash值
- }
- int back_insert_hash(char s[],int len)//计算后缀hash值
- {
- ll sum=;//初始化hash值
- int lenn=strlen(s);//lenn是字符串总长度
- for(int i=lenn-len;i<lenn;i++)//注意这里的len是从后往前数的第几位
- sum=(sum*ins+(ll)s[i])%mod;//计算哈市值
- return sum;//返回hash值
- }
- int main()
- {
- scanf("%s",a);//读入第一个字符串
- scanf("%s",b);//读入第二个字符串
- lena=strlen(a);//第一个字符串的长度
- lenb=strlen(b);//第二个字符串的长度
- for(int i=;i<lena;i++)
- fronta[i]=front_insert_hash(a,i);//字符串a的前缀hash值
- for(int i=;i<lenb;i++)
- backb[i]=back_insert_hash(b,i);//字符串b的后缀hash值
- for(int i=;i<lenb;i++)
- frontb[i]=front_insert_hash(b,i);//字符串b的前缀hash值
- for(int i=;i<lena;i++)
- backa[i]=back_insert_hash(a,i);//字符串a的后缀hash值
- int minlen=min(lena,lenb);//取最短长度
- ans=;//重复长度清0
- for(int i=;i<minlen;i++)//a的头和b的尾
- if(fronta[i]==backb[i])//hash值相等说明字符子串相同
- ans=i;
- maxx=max(ans,maxx);//取最大值
- ans=;//重复长度清0
- for(int i=;i<minlen;i++)//a的尾和b的头
- if(frontb[i]==backa[i])//hash值相等说明字符子串相同
- ans=i;
- maxx=max(ans,maxx);//取最大值
- cout<<maxx;//输出最大覆盖长度
- return ;
- }
这题算是字符串hush板子了吧
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #define mod 1777777777
- using namespace std;
- typedef long long LL;
- char s1[],s2[];
- int len1,len2;
- LL g[];
- LL f1[],f2[];
- void pre()
- {
- g[]=;
- for(int i=;i<max(len1,len2);i++) g[i]=g[i-]*%mod;
- f1[]=s1[]-'a';
- for(int i=;i<len1;i++) f1[i]=(f1[i-]*+s1[i]-'a')%mod;
- f2[]=s2[]-'a';
- for(int i=;i<len2;i++) f2[i]=(f2[i-]*+s2[i]-'a')%mod;
- }
- LL gethash(LL *f,int l,int r)
- {
- if(!l) return f[r];
- return (f[r]-f[l-]*g[r-l+]%mod+mod)%mod;
- }
- int main()
- {
- scanf("%s%s",s1,s2);
- len1=strlen(s1);
- len2=strlen(s2);
- pre();
- for(int i=min(len1,len2);i;i--)
- {
- if(gethash(f1,,i-)==gethash(f2,len2-i,len2-) ) { printf("%d",i); return ; }
- if(gethash(f2,,i-)==gethash(f1,len1-i,len1-) ) { printf("%d",i); return ; }
- }
- printf("");
- }
[USACO09OCT]谷仓里的回声Barn Echoes(hush、STL)的更多相关文章
- 洛谷 2957 [USACO09OCT]谷仓里的回声Barn Echoes
题目描述 The cows enjoy mooing at the barn because their moos echo back, although sometimes not complete ...
- 洛谷——P2957 [USACO09OCT]谷仓里的回声Barn Echoes
https://www.luogu.org/problem/show?pid=2957 题目描述 The cows enjoy mooing at the barn because their moo ...
- [luoguP2957] [USACO09OCT]谷仓里的回声Barn Echoes(Hash)
传送门 团队里的hash水题,数据小的不用hash都能过.. 也就是前缀hash,后缀hash,再比较一下就行. ——代码 #include <cstdio> #include <c ...
- 3409: [Usaco2009 Oct]Barn Echoes 牛棚回声
3409: [Usaco2009 Oct]Barn Echoes 牛棚回声 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 57 Solved: 47[ ...
- [USACO17JAN]Building a Tall Barn建谷仓
题目描述 Farmer John is building a brand new, NNN -story barn, with the help of his KKK cows ( 1≤N≤K≤101 ...
- [luoguP3606] [USACO17JAN]Building a Tall Barn建谷仓(贪心 + 线段树)
传送门 把线段都读进来然后排序,先按右端点为第一关键字从小到大排序,后按左端点为第二关键字从小到大排序. 注意不能先按左端点后按右端点排序,否则会出现大包小的情况,如下: —————— ——— — ...
- 洛谷P3066 [USACO12DEC]逃跑的Barn (线段树合并)
题目描述It's milking time at Farmer John's farm, but the cows have all run away! Farmer John needs to ro ...
- 洛谷P3066 [USACO12DEC] 逃跑的Barn [左偏树]
题目传送门 逃跑的Barn 题目描述 It's milking time at Farmer John's farm, but the cows have all run away! Farmer J ...
- 洛谷 P5542 [USACO19FEB]Painting The Barn
题目传送门 解题思路: 二维差分的板子题.题解传送门 AC代码: #include<iostream> #include<cstdio> using namespace std ...
随机推荐
- [CISCN 2019 初赛]Love Math
0x00 知识点 PHP函数: scandir() 函数:返回指定目录中的文件和目录的数组. base_convert() 函数:在任意进制之间转换数字. dechex() 函数:把十进制转换为十六进 ...
- 阿里巴巴的26款Java开源项目,赶紧戳…
本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...
- elastic启动脚本
#!bin/bash export JAVA_HOME=/usr/java/jdk1.8.0_144 export JRE_HOME=${JAVA_HOME}/jre export CLASSPATH ...
- windows下关闭自动更新方法
第一种方法: 1.windows+R键打开运行窗口,输入services.msc 2.找到windows update服务右键属性 第二种方法 Win键+R键在弹出的运行对话框中输入gpedit.ms ...
- SQL基础教程(第2版)第3章 聚合与排序:练习题
存在以下 个错误. .使用了字符类型的列(product_name)作为 SUM 函数的参数. >> 解答 SUM 函数只能使用数值类型的列作为参数. . WHERE 子句写在了 GROU ...
- PAT Advanced 1099 Build A Binary Search Tree (30) [⼆叉查找树BST]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- java8+tomcate8仅支持TLSv1.2
1.编辑$tomcat_home/conf/server.xml <Connector protocol="org.apache.coyote.http11.Http11NioProt ...
- Physicoochemical|CG content|
NCBI存在的问题: 数据用户的增长 软件开发受限 数据分析缺乏 有些传统束缚,仅用底层语言书写 Pangenome Open gene是随菌株数量增大而增大的gene,Closed gene是随菌株 ...
- Thread--lock,lockInterruptibly,tryLock,tryLock(long timeout, TimeUnit unit)
参考:http://www.dewen.net.cn/q/9077 http://coolxing.iteye.com/blog/1236909 lock,tryLock,lockInterrupti ...
- 微信H5支付,成功样例
<?php/** * Created by PhpStorm. * User: Administrator * Date: 2019/6/3 * Time: 12:00 */ if( !defi ...