hdu 5745(2016 Multi-University Training Contest 2)

La Vie en rose

Time Limit: 14000/7000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 528    Accepted Submission(s): 257


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:

1. select some indices   i1,i2,...,ik  such that   1i1<i2<...<ik<|p|  and   |ijij+1|>1  for all   1j<k .
2. swap   pij  and   pij+1  for all   1jk .

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   (1n105,1mmin{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+m1  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
 

Author
zimpha
 

Source


题意:一个母串s,一个字串p,求p串是否能变成s的某一子串,p可以交换任意两个相邻字符,每个字符只能交换一次;最后得到一个二进制串,1表示s串从该位置为起始点,p变成与之一样;0反之。(据说比赛的时候好多人骂这题,说是条件1是可以挑两个字符交换......反正那句我没看懂,然后没管它....就那么AC了.....)

分析:我的想法是,先判断p和在s中对应的字串的某值(不知道怎么解释,因为是自己定义的.....)是否一样,不一样说明无论p怎么变都不可能变成s的那个字串,一样再继续,一位一位的判断,如果不一样就判断后一位是否和当前一样........总之也就是暴力一遍......

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 1e9;
const int MOD = 1e9+7;
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
#define lson (i<<1)
#define rson ((i<<1)|1)
#define N 100010

int n,m;
int sum_s[N],sum_p[N];
char s[N],p[5005];

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        scanf("%s%s",s+1,p+1);
        sum_s[0] = sum_p[0] = 0;
        for(int i=1; i<=n; i++)
            sum_s[i] = sum_s[i-1]+s[i]-'a';
        for(int i=1; i<=m; i++)
            sum_p[i] = sum_p[i-1]+p[i]-'a';
        bool flag;
        int pp = sum_p[m];
        for(int i=m; i<=n; i++)
        {
            int ss = sum_s[i]-sum_s[i-m];

            flag = true;
            if(ss == pp)
            {
                for(int j=i-m+1; j<=i; j++)
                {
                    if(s[j] != p[m+j-i])
                    {
                        if(j+1<=i&&s[j]==p[m+j-i+1]&&s[j+1]==p[m+j-i])
                        {
                            j++;
                        }
                        else {flag = false; break;}
                    }
                }
            }
            else flag = false;

            if(flag) printf("1");
            else printf("0");
        }
        for(int i=n-m+1; i<n; i++)
            printf("0");
        printf("\n");
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值