匹配字符串

题意大概是这样的:

有两个串吧,子串可以存在 ‘?’, ‘?’可以和任意字符匹配,然后还有一个母串,判断母串是否包含子串,也就是子串是否包含于母串
例如 子串:aa?bbcc 母串:aabbbcc
这就是符合要求的串
( 由于版权问题,不提供原题 )

思路大概是这样的

这就是kmp啊,只是匹配的时候多了一个条件(当出现 ‘?’时直接跳过此位)
就是那层if里面加一个s[t2]=='?'

代码大概是这样的

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
char t[100101],s[100101];
int next[100101];
int flag;
int main()
{
    freopen("trigger.in","r",stdin);
    freopen("trigger.out","w",stdout);
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        flag=0;
        cin>>s>>t;
        int len=strlen(s);
        int t1=0,t2=-1;
        next[0]=-1;

        while(t1<len)
        {
            if(t2==-1||s[t1]==s[t2]||s[t2]=='?')
            {
                next[++t1]=++t2;
            }
            else t2=next[t2];
        }

        int len1=strlen(t),len2=strlen(s);
        t1=0,t2=0;

        while(t1<len1)
        {
            if(t2==-1||t[t1]==s[t2]||s[t2]=='?')
                t1++,t2++;
            else
                t2=next[t2];
            if(t2==len2) 
            {
                flag=1;
                printf("I can AK!\n");
                break;
            } 
        }
        if(!flag) printf("I will WA!\n");

    }

    return 0; 
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值