日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

杭电ACM-LCY算法进阶培训班-专题训练(KMP)

發布時間:2024/3/7 编程问答 62 豆豆
生活随笔 收集整理的這篇文章主要介紹了 杭电ACM-LCY算法进阶培训班-专题训练(KMP) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

杭電ACM-LCY算法進階培訓班-專題訓練(KMP)

  • 杭電ACM-LCY算法進階培訓班-專題訓練(KMP)
    • 剪花布條
      • Problem Description
      • Input
      • Output
      • Sample Input
      • Sample Output
      • 思路
    • Simpsons’ Hidden Talents
      • Problem Description
      • Input
      • Output
      • Sample Input
      • Sample Output
      • 思路

剪花布條

Problem Description

一塊花布條,里面有些圖案,另有一塊直接可用的小飾條,里面也有一些圖案。對于給定的花布條和小飾條,計算一下能從花布條中盡可能剪出幾塊小飾條來呢?

Input

輸入中含有一些數據,分別是成對出現的花布條和小飾條,其布條都是用可見ASCII字符表示的,可見的ASCII字符有多少個,布條的花紋也有多少種花樣。花紋條和小飾條不會超過1000個字符長。如果遇見#字符,則不再進行工作。

Output

輸出能從花紋布中剪出的最多小飾條個數,如果一塊都沒有,那就老老實實輸出0,每個結果之間應換行。

Sample Input

abcde a3 aaaaaa aa #

Sample Output

0 3

思路

KMP模板,每次匹配成功后,ans++記錄答案數,令jjj=0防止重復匹配。

#include<iostream> using namespace std; string a,b; int n,m,p[1005],ans;void pre(){p[1]=0;for(int i=1,j=0;i<m;i++){while(j>0&&b[i+1]!=b[j+1]) j=p[j];if(b[i+1]==b[j+1]) j++;p[i+1]=j;} }int main(){while(cin>>a,a!="#"){cin>>b;n=a.size(); m=b.size(); ans=0;a.insert(0," "); b.insert(0," ");pre();for(int i=0,j=0;i<n;i++){while(j>0&&a[i+1]!=b[j+1]) j=p[j];if(a[i+1]==b[j+1]) j++;if(j==m) ans++,j=0;}cout<<ans<<"\n";} }

2021/8/4修改模板,重做這道題

#include<bits/stdc++.h> using namespace std; const int N=1010; int n,m,p[N]; char a[N],b[N];void pre(){p[1]=0;for(int i=1,j=0;i<=m;i++){while(j&&b[i+1]!=b[j+1]) j=p[j];if(b[i+1]==b[j+1]) j++;p[i+1]=j;} }int main(){while(scanf("%s",a+1),a[1]!='#'){int ans=0;scanf("%s",b+1);n=strlen(a+1),m=strlen(b+1);pre();for(int i=0,j=0;i<=n;i++){while(j&&a[i+1]!=b[j+1]) j=p[j];if(a[i+1]==b[j+1]) j++;if(j==m) ans++,j=0;}printf("%d\n",ans);} }

Simpsons’ Hidden Talents

Problem Description

Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had.
Marge: Yeah, what is it?
Homer: Take me for example. I want to find out if I have a talent in politics, OK?
Marge: OK.
Homer: So I take some politician’s name, say Clinton, and try to find the length of the longest prefix
in Clinton’s name that is a suffix in my name. That’s how close I am to being a politician like Clinton
Marge: Why on earth choose the longest prefix that is a suffix???
Homer: Well, our talents are deeply hidden within ourselves, Marge.
Marge: So how close are you?
Homer: 0!
Marge: I’m not surprised.
Homer: But you know, you must have some real math talent hidden deep in you.
Marge: How come?
Homer: Riemann and Marjorie gives 3!!!
Marge: Who the heck is Riemann?
Homer: Never mind.
Write a program that, when given strings s1 and s2, finds the longest prefix of s1 that is a suffix of s2.

Input

Input consists of two lines. The first line contains s1 and the second line contains s2. You may assume all letters are in lowercase.

Output

Output consists of a single line that contains the longest string that is a prefix of s1 and a suffix of s2, followed by the length of that prefix. If the longest such string is the empty string, then the output should be 0.
The lengths of s1 and s2 will be at most 50000.

Sample Input

clinton homer riemann marjorie

Sample Output

0 rie 3

思路

題意為,給兩個字符串s1s1s1s2s2s2,求一個最長的字符串s3s3s3,使得s3s3s3s1s1s1的前綴,同時為s2s2s2的后綴。
s1s1s1s2s2s2拼接起來,然后求它的Next數組(P數組)即可。
注意:s3s3s3的長度應該小于s1s1s1s2s2s2,需要判斷Next[N]Next[N]Next[N]是否小于s1s1s1s2s2s2的長度。

#include<iostream> using namespace std; const int maxn=1e5+5; string a,b; int p[maxn],n;int main(){while(cin>>a>>b){int minn=0;minn=min(a.size(),b.size());a=" "+a+b; n=a.size()-1;p[1]=0;for(int i=1,j=0;i<n;i++){while(j>0&&a[i+1]!=a[j+1]) j=p[j];if(a[i+1]==a[j+1]) j++;p[i+1]=j;}if(p[n]) cout<<a.substr(1,min(p[n],minn))<<" ";cout<<min(p[n],minn)<<"\n";} }

總結

以上是生活随笔為你收集整理的杭电ACM-LCY算法进阶培训班-专题训练(KMP)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。