杭电ACM-LCY算法进阶培训班-专题训练(KMP)
杭電ACM-LCY算法進階培訓(xùn)班-專題訓(xùn)練(KMP)
- 杭電ACM-LCY算法進階培訓(xùn)班-專題訓(xùn)練(KMP)
- 剪花布條
- Problem Description
- Input
- Output
- Sample Input
- Sample Output
- 思路
- Simpsons’ Hidden Talents
- Problem Description
- Input
- Output
- Sample Input
- Sample Output
- 思路
剪花布條
Problem Description
一塊花布條,里面有些圖案,另有一塊直接可用的小飾條,里面也有一些圖案。對于給定的花布條和小飾條,計算一下能從花布條中盡可能剪出幾塊小飾條來呢?
Input
輸入中含有一些數(shù)據(jù),分別是成對出現(xiàn)的花布條和小飾條,其布條都是用可見ASCII字符表示的,可見的ASCII字符有多少個,布條的花紋也有多少種花樣。花紋條和小飾條不會超過1000個字符長。如果遇見#字符,則不再進行工作。
Output
輸出能從花紋布中剪出的最多小飾條個數(shù),如果一塊都沒有,那就老老實實輸出0,每個結(jié)果之間應(yīng)換行。
Sample Input
abcde a3 aaaaaa aa #Sample Output
0 3思路
KMP模板,每次匹配成功后,ans++記錄答案數(shù),令jjj=0防止重復(fù)匹配。
#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 marjorieSample Output
0 rie 3思路
題意為,給兩個字符串s1s1s1和s2s2s2,求一個最長的字符串s3s3s3,使得s3s3s3為s1s1s1的前綴,同時為s2s2s2的后綴。
將s1s1s1和s2s2s2拼接起來,然后求它的Next數(shù)組(P數(shù)組)即可。
注意:s3s3s3的長度應(yīng)該小于s1s1s1和s2s2s2,需要判斷Next[N]Next[N]Next[N]是否小于s1s1s1和s2s2s2的長度。
總結(jié)
以上是生活随笔為你收集整理的杭电ACM-LCY算法进阶培训班-专题训练(KMP)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 给大家推荐一个比较好的VC论坛【VC驿站
- 下一篇: 浙大高分子物理郑强教授的激情演讲