zoukankan      html  css  js  c++  java
  • [USACO09OCT]谷仓里的回声Barn Echoes(hush、STL)

    https://www.luogu.org/problem/P2957

    题目描述

    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

    输出

    11

    说明

    'abcxxxxabcx' is a prefix of the first string and a suffix of the second string.

    题目大意

    给两个字符串,找出最大重复长度(两个字符串的重复部份指的是同时是一个字符串的前缀和另一个字符串的后缀的字符串)

    暴力解法:

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 #include <string>
     5 #include <math.h>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <queue>
     9 #include <set>
    10 #include <stack>
    11 #include <map>
    12 #include <math.h>
    13 const int INF=0x3f3f3f3f;
    14 typedef long long LL;
    15 const int mod=1e9+7;
    16 const int maxn=1e5+10;
    17 using namespace std;
    18 //ios::sync_with_stdio(false);
    19 //    cin.tie(NULL);
    20 
    21 int main()
    22 {
    23     string s1,s2;
    24     cin>>s1>>s2;
    25     int len1=s1.size();
    26     int len2=s2.size();
    27     string ss="";
    28     int ans=0;
    29     for(int i=0;i<=len1;i++)
    30     {
    31         ss+=s1[i];
    32         if(i+1>len2)
    33             break;
    34         if(s2.find(ss,len2-(i+1))!=string::npos)
    35             ans=i+1;
    36     }
    37     ss="";
    38     for(int i=0;i<=len2;i++)
    39     {
    40         ss+=s2[i];
    41         if(i+1>len1)
    42             break;
    43         if(s1.find(ss,len1-(i+1))!=string::npos)
    44             ans=max(ans,i+1);
    45     }
    46     cout<<ans<<endl;
    47 }

     思路一样,但更加简洁的写法

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 #include <string>
     5 #include <math.h>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <queue>
     9 #include <set>
    10 #include <stack>
    11 #include <map>
    12 #include <math.h>
    13 const int INF=0x3f3f3f3f;
    14 typedef long long LL;
    15 const int mod=1e9+7;
    16 const int maxn=1e5+10;
    17 using namespace std;
    18 //ios::sync_with_stdio(false);
    19 //    cin.tie(NULL);
    20 
    21 int main()
    22 {
    23     string s1,s2;
    24     cin>>s1>>s2;
    25     int ans=0;
    26     for(int i=1;i<=min(s1.size(),s2.size());i++)//比较s1与s2长度,并取最小
    27     {
    28         if(s1.substr(0,i)==s2.substr(s2.size()-i,i))//提取子串
    29             ans=max(ans,i);
    30         if(s2.substr(0,i)==s1.substr(s1.size()-i,i))
    31             ans=max(ans,i);
    32     }
    33     cout<<ans<<endl;
    34 }

    下面是别人的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值相同的最大位置,就是最长的重复部分。

    代码

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #define ll long long
     5 #define mod 192**817//咳咳
     6 #define ins 11111
     7 using namespace std;
     8 char a[85];//字符串a 
     9 char b[85];//字符串b 
    10 int lena,lenb;//两个字符串的长度 
    11 int fronta[85],backa[85];//a的前缀、后缀hash值 
    12 int frontb[85],backb[85];//b的前缀、后缀hash值 
    13 int ans;//记录覆盖长度 
    14 int maxx;//记录最大覆盖长度 
    15 int front_insert_hash(char s[],int len)//计算前缀hash值 
    16 {
    17     ll sum=0;//初始化hash值 
    18     for(int i=0;i<len;i++)
    19         sum=(sum*ins+(ll)s[i])%mod;//计算hash值 
    20     return sum;//返回hash值 
    21 }
    22 int back_insert_hash(char s[],int len)//计算后缀hash值 
    23 {
    24     ll sum=0;//初始化hash值 
    25     int lenn=strlen(s);//lenn是字符串总长度 
    26     for(int i=lenn-len;i<lenn;i++)//注意这里的len是从后往前数的第几位 
    27         sum=(sum*ins+(ll)s[i])%mod;//计算哈市值 
    28     return sum;//返回hash值 
    29 }
    30 int main()
    31 {
    32     scanf("%s",a);//读入第一个字符串 
    33     scanf("%s",b);//读入第二个字符串 
    34     lena=strlen(a);//第一个字符串的长度 
    35     lenb=strlen(b);//第二个字符串的长度 
    36     for(int i=0;i<lena;i++)
    37         fronta[i]=front_insert_hash(a,i);//字符串a的前缀hash值 
    38     for(int i=0;i<lenb;i++)
    39         backb[i]=back_insert_hash(b,i);//字符串b的后缀hash值
    40     for(int i=0;i<lenb;i++)
    41         frontb[i]=front_insert_hash(b,i);//字符串b的前缀hash值 
    42     for(int i=0;i<lena;i++)
    43         backa[i]=back_insert_hash(a,i);//字符串a的后缀hash值  
    44     int minlen=min(lena,lenb);//取最短长度 
    45     ans=0;//重复长度清0 
    46     for(int i=0;i<minlen;i++)//a的头和b的尾 
    47         if(fronta[i]==backb[i])//hash值相等说明字符子串相同 
    48             ans=i;
    49     maxx=max(ans,maxx);//取最大值 
    50     ans=0;//重复长度清0 
    51     for(int i=0;i<minlen;i++)//a的尾和b的头 
    52         if(frontb[i]==backa[i])//hash值相等说明字符子串相同 
    53             ans=i;
    54     maxx=max(ans,maxx);//取最大值
    55     cout<<maxx;//输出最大覆盖长度 
    56     return 0;
    57 }

    这题算是字符串hush板子了吧

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #define mod 1777777777
     5 using namespace std;
     6 typedef long long LL;
     7 char s1[81],s2[82];
     8 int len1,len2;
     9 LL g[82];
    10 LL f1[82],f2[82];
    11 void pre()
    12 {
    13     g[0]=1;
    14     for(int i=1;i<max(len1,len2);i++) g[i]=g[i-1]*26%mod;
    15     f1[0]=s1[0]-'a';
    16     for(int i=1;i<len1;i++) f1[i]=(f1[i-1]*26+s1[i]-'a')%mod;
    17     f2[0]=s2[0]-'a';
    18     for(int i=1;i<len2;i++) f2[i]=(f2[i-1]*26+s2[i]-'a')%mod;    
    19 }
    20 LL gethash(LL *f,int l,int r)
    21 {
    22     if(!l) return f[r];  
    23     return (f[r]-f[l-1]*g[r-l+1]%mod+mod)%mod;
    24 }
    25 int main()
    26 {
    27     scanf("%s%s",s1,s2);
    28     len1=strlen(s1);
    29     len2=strlen(s2);
    30     pre();
    31     for(int i=min(len1,len2);i;i--)
    32     {
    33         if(gethash(f1,0,i-1)==gethash(f2,len2-i,len2-1) ) { printf("%d",i); return 0; }
    34         if(gethash(f2,0,i-1)==gethash(f1,len1-i,len1-1) ) { printf("%d",i); return 0; }
    35     }
    36     printf("0");
    37 }
  • 相关阅读:
    Script:Generate A DDL Script For A Table
    如何在windows vista/2008/7中 安装Oracle OMS 即Grid Control
    Sqlserver2005迁移至Oracle系列之五:角色、用户、及权限
    Mysql:命令选项、配置选项、(全局、会话)系统变量、状态变量:SQL模式
    Mysql:命令选项、配置选项、(全局、会话)系统变量、状态变量:如何使用系统变量?
    Mysql:临时表、表变量
    Sqlserver2005迁移至Oracle系列之四:在Oracle中创建位或运算函数bitor
    flex 图片旋转
    基于模板和XML在BS结构应用中生成word文件
    操作图片文件写入word
  • 原文地址:https://www.cnblogs.com/jiamian/p/11434770.html
Copyright © 2011-2022 走看看