POJ 1936 All in All 匹配, 水题 难度:0

本文介绍了一种简单有效的方法来解决POJ 1936题目中的子串判断问题,通过使用计数器进行匹配,判断字符串A是否为字符串B的子串。

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

题目

http://poj.org/problem?id=1936


题意

多组数据,每组数据有两个字符串A,B,求A是否是B的子串。(注意是子串,也就是不必在B中连续)

思路

设置计数器cnt为当前已匹配A的长度,明显在扫描B的过程中只需要记住cnt这一个状态。

扫描B,每次与A[cnt]匹配就将计数器增加1,cnt与A的长度一致时A就是B的子串。

感想

这道题也许可以用更复杂的方法。

代码

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 #include <sstream>
 6 using namespace std;
 7 typedef long long ll;
 8 const int maxn = 1e6 + 6;
 9 int nxt[maxn];
10 char pat[maxn];
11 char maz[maxn];
12 
13 bool solve(){
14     int cnt = 0;
15     for(int i = 0;maz[i];i++){
16         if(maz[i] == pat[cnt])cnt++;
17         if(pat[cnt] == 0)return true;
18     }
19     return false;
20 }
21 
22 int main(){
23 #ifdef LOCAL
24     freopen("input.txt","r",stdin);
25 #endif // LOCAL
26     for(int i = 0;scanf("%s%s",pat,maz)==2;i++){
27         bool ans = solve();
28         if(ans){
29             puts("Yes");
30         }else{
31             puts("No");
32         }
33     }
34 
35     return 0;
36 }
View Code

 

转载于:https://www.cnblogs.com/xuesu/p/6375567.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值