寒假前刷题(4)kmp系列 hdu 2087

本文详细介绍了如何利用KMP算法解决在给定的花布条和小饰条中尽可能剪出最多小饰条的问题。通过分析问题背景和输入输出规范,阐述了算法实现过程,并提供了关键代码片段,旨在帮助读者理解并掌握KMP算法在实际问题中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案。对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢?
 

Input
输入中含有一些数据,分别是成对出现的花布条和小饰条,其布条都是用可见ASCII字符表示的,可见的ASCII字符有多少个,布条的花纹也有多少种花样。花纹条和小饰条不会超过1000个字符长。如果遇见#字符,则不再进行工作。
 

Output
输出能从花纹布中剪出的最多小饰条个数,如果一块都没有,那就老老实实输出0,每个结果之间应换行。
 

Sample Input
abcde a3 aaaaaa aa #
 

Sample Output
0 3
 
//这道kmp的关键就是在一次匹配后不能退出,必须匹配到母串结束为止。
下面是代码:

#include<stdio.h> #include<string.h> #include<stdlib.h> #include<iostream>

using namespace std;

char s1[1005]; char s2[1005]; int next[1005]; int l1,l2;

void getnextval(char *T,int *next) {      int  k=1,j=0;      while(k<l2)      {           if(j==0||T[j]==T[k])             {                  ++j;                  ++k;                  if(T[k]==T[j])                     next[k]=next[j];                  else                     next[k]=j;             }             else                j=next[j];      } }

int kmp(char *T,char *p,int *next) {      int k=0,j=0,count=0;      while(k<=l1)      {           if(j==0||T[k]==p[j])           {                if(j==l2)                {                     count++;                     j=0;                }                ++k;                ++j;           }                else                   j=next[j];

     }      return count; }

int main() {      while(scanf("%s",s1+1),s1[1]!='#')      {           l1=strlen(s1+1);           scanf("%s",s2+1);           l2=strlen(s2+1);           getnextval(s2,next);           printf("%d\n",kmp(s1,s2,next));      }      return 0; }

 
### 关于 Codeforces 平台上的 KMP 算法练习目 对于希望在 Codeforces 上找到有关 KMP (Knuth-Morris-Pratt) 字符串匹配算法的练习目的选手来说,可以关注几个特定标签下的问。通常这些目会被标记为 "strings" 或者更具体地标记为 "string suffix structures"[^1]。 为了帮助更好地理解如何查找适合的练习目,在此提供一段 Python 代码用于模拟访问 API 获取带有指定标签的问列表: ```python import requests def fetch_problems(tag, platform="codeforces"): url = f"https://{platform}.com/api/problemset.problems?tags={tag}" response = requests.get(url) if response.status_code != 200: raise Exception(f"Failed to retrieve data from {platform}") json_data = response.json() problem_list = [] for index, item in enumerate(json_data[&#39;result&#39;][&#39;problems&#39;]): name = item["name"] rating = item.get(&#39;rating&#39;, &#39;N/A&#39;) contest_id = item["contestId"] index = item["index"] problem_info = { "Name": name, "Rating": rating, "Link": f"{platform}.com/contest/{contest_id}/problem/{index}" } problem_list.append(problem_info) return problem_list[:5] # Example usage with the tag related to string processing or specifically KMP pattern matching. kmp_related_tag = "strings" fetched_problems = fetch_problems(kmp_related_tag) for prob in fetched_problems: print(prob) ``` 这段脚本会调用 Codeforces 的公开 API 来获取五个与字符串处理相关的编程挑战链接,并打印出来供参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值