hdu5745 La Vie en rose (字符串)

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 ,每次可以选择 Pij Pij+1 交换。

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

ac代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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;iint bz=0;int k=i;
            if(i+m>n)
            {
                printf("0");
                continue;
            }
            for(int j=0;jif(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;
}

你可能感兴趣的:(水题)