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 1≤i1<i2<...<ik<|p| and |ij−ij+1|>1 for all 1≤j<k .
2. swap pij and pij+1 for all 1≤j≤k .
Now, for a given a string s=s1s2...sn , Professor Zhang wants to find all occurrences of all the generated patterns in s .
1. select some indices i1,i2,...,ik such that 1≤i1<i2<...<ik<|p| and |ij−ij+1|>1 for all 1≤j<k .
2. swap pij and pij+1 for all 1≤j≤k .
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.
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
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;
}