hdu5745 La Vie en rose (字符串)

本文介绍了一个模式匹配问题,通过给定的字符串A和模式串B,采用特定规则进行字符交换生成多种模式,并在A中查找所有可能的模式出现位置。文章提供了一种O(nm)时间复杂度的直接匹配解决方案。

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

La Vie en rose
Time Limit: 14000/7000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 883 Accepted Submission(s): 478

Problem Description
Professor Zhang would like to solve the multiple pattern matching problem, but he only has only one pattern string p=p1p2…pm. So, he wants to generate as many as possible pattern strings from p using the following method:

Now, for a given a string s=s1s2…sn, Professor Zhang wants to find all occurrences of all the generated patterns in s.

Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains two integers n and m (1≤n≤105,1≤m≤min{5000,n}) – the length of s and p.

The second line contains the string s and the third line contains the string p. Both the strings consist of only lowercase English letters.

Output
For each test case, output a binary string of length n. The i-th character is “1” if and only if the substring sisi+1…si+m−1 is one of the generated patterns.

Sample Input

3
4 1
abac
a
4 2
aaaa
aa
9 3
abcbacacb
abc

Sample Output

1010
1110
100100100

题意:给你一个长为n的串A和一个长为m的串B,然后从头开始匹配,如果可以匹配,则当前输出1,如果不能输出0,可以在B串中选取一些下标ij,其中|ijij+1|>1,每次可以选择PijPij+1交换。

思路:可以直接暴力扫匹配,复杂度O(nm),如果当前字符匹配,则继续,如果不匹配,则检查当前A串字符是否和B串当前字符后面的那个字符匹配,还有当前A串字符后面的字符是否和B串当前字符匹配,如果满足,则跳过继续,如果不满足,则退出匹配。

ac代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0x7fffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
//head
char s[100001],p[5001];
int main()
{
    int t;scanf("%d",&t);
    int cas=0;
    while(t--)
    {
        int n,m;scanf("%d%d",&n,&m);
        scanf("%s%s",s,p);
        for(int i=0;i<n;i++)
        {
            int bz=0;int k=i;
            if(i+m>n)
            {
                printf("0");
                continue;
            }
            for(int j=0;j<m;j++)
            {
                if(s[k]!=p[j])
                {
                    if(s[k]==p[j+1]&&s[k+1]==p[j])
                        j++,k+=2;
                    else
                    {
                        bz=1;
                        break;
                    }
                }
                else
                    k++;
            }
            if(bz)
                printf("0");
            else
                printf("1");
        }
        printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值