题目描述

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

输入描述:

* 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.

解答

这道题可以用字符串做,也可以用字符串哈希做,而在这篇题解中,博客主要用字符串哈希(不会的可以先去看看)来做这道题。
我们分别给两个字符串记一个哈希值前缀和和哈希值后缀和,在两两比较,哈希值相同就是同一字符串,用ans更新最大值即可。
这里注意一下存字符串前缀和和存字符串后缀和不一样的方式:
前缀和:已知999,当继续向后加x,应该999*10+x;
后缀和:已知999,当继续向前加x,应该x*1000+999;
代码:
#include<bits/stdc++.h>
   #define FAST std::ios::sync_with_stdio(false),std::cin.tie(0),std::cout.tie(0)//优化输入输出,更方便 
   typedef unsigned long long ull;
   using namespace std;
   string x,y;
   ull hash1[2][1001];//0是字符串x的前缀和,1是字符串y的前缀和 
   ull hash2[2][1001];//0是字符串x的后缀和,1是字符串y的后缀和 
   ull mod=212370440130137957ll;//模 
   ull base=11;//进制,在这里就定义一个小的进制了,因为数值过大不方便 
  int main()
  {
      FAST;
      cin>>x>>y;
      //前缀和操作 
      hash1[0][0]=(int)(x[0]);//第一位直接存 
    hash1[1][0]=(int)(y[0]);
      for(int i=1;i<x.size();i++)//每一位是前面的*base后加上它 
     {
          hash1[0][i]=hash1[0][i-1]*base+(int)(x[i]);
          hash1[0][i]%=mod;//边乘边余 
      } 
      for(int i=1;i<y.size();i++)//同上 
     {
          hash1[1][i]=hash1[1][i-1]*base+(int)(y[i]);
          hash1[1][i]%=mod;
      }
      
      hash2[0][0]=x[x.size()-1];//最后一位直接存 
     hash2[1][0]=y[y.size()-1];
      for(int i=x.size()-2;i>=0;i--)//注意,因为是后缀和,所以要倒着来 
      {
          int qwe=x.size()-i-1;
          ull sum=x[i];
          while(qwe--)//后缀和
          {
              sum*=base;
             sum%=mod;
          }
         hash2[0][x.size()-i-1]=hash2[0][x.size()-i-2]+sum;
         hash2[0][x.size()-i-1]%=mod;
      } 
      for(int i=y.size()-2;i>=0;i--)//同上 
      {
         int qwe=y.size()-i-1;
          ull sum=y[i];
         while(qwe--)
          {
              sum*=base;
              sum%=mod;
         }
         hash2[1][y.size()-i-1]=hash2[1][y.size()-i-2]+sum;
         hash2[1][y.size()-i-1]%=mod;
      } 
     int ans=0;
      for(int i=0;i<min(x.size(),y.size());i++)
     {
          if(hash2[0][i]==hash1[1][i])ans=max(ans,i);//如果相等,就取最优值 
          if(hash1[0][i]==hash2[1][i])ans=max(ans,i);
      }
      cout<<ans+1;//因为字符串从0开始,所以要加一 
      
  }


来源:华恋~韵