当前位置:网站首页>[hdu] p2087 cut cloth strip
[hdu] p2087 cut cloth strip
2022-06-23 01:23:00 【Lupin123123】
kmp Do not overlap matching template questions . Topic link
In a normal kmp Matching , Overlap matching can occur . If no overlap is required , The following changes should be made :
void work()
{
int j=0;
for (int i=1; i<=len1; i++)
{
while(j && s2[j+1]!=s1[i]) j=p[j];
if (s2[j+1]==s1[i]) j++;
if (j==len2)
{
ans++;
j=0; // It was originally j=p[j];
}
}
}
Complete code :
#include<bits/stdc++.h>
#define FAST ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define INF 0x3f3f3f3f
typedef long long ll;
const int maxn = 1e5+5;
using namespace std;
char s1[maxn],s2[maxn];
int len1,len2,ans;
int p[maxn];
void get()
{
int j=0;
p[1]=0;
for (int i=2; i<=len2; i++)
{
while(j && s2[j+1]!=s2[i]) j=p[j];
if (s2[j+1]==s2[i]) j++;
p[i]=j;
}
}
void work()
{
ans=0;
int j=0;
for (int i=1; i<=len1; i++)
{
while(j && s2[j+1]!=s1[i]) j=p[j];
if (s2[j+1]==s1[i]) j++;
if (j==len2)
{
ans++;
j=0;
}
}
}
int main()
{
FAST;
while(cin>>s1+1)
{
if (s1[1]=='#') break;
cin>>s2+1;
len1=strlen(s1+1);
len2=strlen(s2+1);
memset(p,0,sizeof(int)*(len2+10));
get();
work();
cout<<ans<<endl;
}
return 0;
}
边栏推荐
- 贵金属现货白银如何呢?
- The devil cold rice # 099 the devil said to travel to the West; The nature of the boss; Answer the midlife crisis again; Specialty selection
- LINQ 查詢
- 3D printing microstructure
- 总体标准差和样本标准差
- Overview of visual object detection technology based on deep learning
- How to set the power-off auto start of easycvr hardware box
- Shell view help
- [UVM] don't say that your VIP can't use ral model
- Xiaobai operates win10 to expand Disk C (allocate disk D memory to Disk C) and the test is valid for many times
猜你喜欢

New progress in the construction of meituan's Flink based real-time data warehouse platform
![[sliding window] leetcode992 Subarrays with K Different Integers](/img/69/1ac0c54d33af0f7a9e3db3e82d076b.png)
[sliding window] leetcode992 Subarrays with K Different Integers

黄金etf持仓量如何算

How to refine permissions to buttons?

What is the storage structure and mode of data in the database?

MySQL-Seconds_ behind_ Master accuracy error

Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + commercial realization

3D printing microstructure
![Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;](/img/75/d2ad171d49611a6578faf2d390af29.jpg)
Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;

Ros2 summer school 2022 transfer-
随机推荐
Wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe
Local deployment and problem solving of IIS in ArcGIS JS 4.23
SAP ui5 application development tutorial 103 - how to consume third-party libraries in SAP ui5 applications
【机器学习-西瓜书】更文挑战【Day1】:1.1 引言
崔鹏团队:万字长文梳理「稳定学习」全景图
Const defined variables and for of and for in in JS
C# SerializableDictionary序列化/反序列化
LINQ query
Add / get and delete cookies
魔王冷饭||#099 魔王说西游;老板的本质;再答中年危机;专业选择
LeetCode刷题——715. Range 模块
Software construction course ADT and OOP understanding
leetcode 91. Decode Ways 解码方法(中等)
Pat class A - 1013 battle over cities
Swiftui swift tutorial 14 useful array operators
OSPF comprehensive experiment
Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;
cadence SPB17.4 - allegro - 優化指定單條電氣線折線連接角度 - 折線轉圓弧
E-R图
How to set the power-off auto start of easycvr hardware box