hdu 5745 la vie en rose

这道题的官方题解是dp,但是可以暴力出来。改天再研究怎么dp。

暴力的时候,如果计算sum的时候,调用strlen函数会超时,可见这个函数并不是十分的好。以后能不用尽量不用。

La Vie en rose

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

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.

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

Author

zimpha

Source

2016 Multi-University Training Contest 2

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstring>
#include<string>
using namespace std;
const int maxx=100005;
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n,m;
        char s[maxx];
        char p[maxx];
        scanf("%d%d",&n,&m);
        scanf("%s",s);
        scanf("%s",p);
        int sump[maxx];
        int sums[maxx];
        sump[0]=sums[0]=0;
        for(int i=1;i<=n;i++){
            sums[i]=s[i-1]-‘a‘+sums[i-1];
        }
        for(int i=1;i<=m;i++){
            sump[i]=p[i-1]-‘a‘+sump[i-1];
        }
        int pis=sump[m];
        int shave=sums[m];
        for(int i=m;i<=n;i++){
            shave=sums[i]-sums[i-m];
           // cout<<"shave: "<<shave<<endl;
            if(pis==shave){
                int sta=i-m;
                int pos=sta;
                int flag=1;
                for(int j=0;pos<i;j++){
                    if(s[pos]==p[j]){
                        pos++;
                    }else{
                        if(pos+1<i&&s[pos+1]==p[j]&&s[pos]==p[j+1]){
                            pos+=2;
                            j++;
                        }else{
                            flag=0;
                            break;
                        }
                    }
                }
                if(flag){
                    printf("1");
                }else{
                    printf("0");
                }
            }else{
                printf("0");
            }
        }
        for(int i=m-1;i>0;i--){
            printf("0");
        }
        printf("\n");

    }
    return 0;
}

时间: 2024-08-01 22:47:33

hdu 5745 la vie en rose的相关文章

hdu 5745 La Vie en rose(dp+bitset)

题目链接:hdu 5745 La Vie en rose 题意: 给两个字符串 a 和 b ,b可以进行变换,规则是可以任意交换相邻两个字符的位置,但是不可以有交叉(例如3和4交换,5和6交换 互不影响,但是2和3,3和4就不可以).求a中每一个位置能不能匹配b或b变换得到的子串. 题解: 考虑dp,dp[i][j][k]表示a[i]和b[j]匹配,k为1表示j未做交换,k=0表示j和j-1进行交换,k=2表示j和j+1进行交换. 然后bitset优化一下常数. dp方程为: dpi,j,0=d

HDU 5745 La Vie en rose (DP||模拟) 2016杭电多校联合第二场

题目:传送门. 这是一道阅读理解题,正解是DP,实际上模拟就能做.pij+1 指的是 (pij)+1不是 pi(j+1),判断能否交换输出即可. #include <iostream> #include <algorithm> #include <cstdio> #include<cstring> using namespace std; int t,n; char str1[100009],str2[5009]; char tmp[5009]; int m

La ve en rose 玫瑰人生

玫瑰人生是是法国著名女歌手爱迪特的一首代表作品,这首歌是她对自己人生的概括,虽然她一生遭逢诸多不幸(这些都在她的作品中表现了),但她的这首玫瑰人生却仍然透过爱情的方式来展示她自己快乐幸福的人生态度.这首歌在法国流传甚广,刘欢和苏菲玛索在去年的春晚上演唱过它.下面是这首歌的歌词和译文. Des yeux qui font baiser les miens 他的双唇吻我的眼 Un rire qui se perd sur sa bouche 一抹微笑掠过他的嘴边 Voilà le portrait

hdu5745--La Vie en rose (DP+bitset)

好题,学到新姿势! 题意:给两个字符串 a 和 b ,b可以进行变换,规则是可以任意交换相邻两个字符的位置,但是不可以有交叉(例如3和4交换,5和6交换 互不影响,但是2和3,3和4就不可以).求a中每一个位置能不能匹配b或b变幻得到的子串. 题解:考虑dp.dp[i][j][k]表示a[i]和b[j]匹配,k为1表示j未做交换,k=0表示j和j-1进行交换,k=2表示j和j+1进行交换. 这样写出来的代码: #include <iostream> #include <cstdio>

HOBBIES-LA VIE EN ROSE

 http://www.acfun.tv/a/aa3929900 http://www.acfun.tv/a/aa3930098 http://www.acfun.tv/a/aa3930298 http://www.acfun.tv/a/aa3930494 http://www.acfun.tv/a/aa3930845 http://www.acfun.tv/a/aa3931039 http://www.acfun.tv/a/aa3931231 http://www.acfun.tv/a/a

2016 Multi-University Training Contest 2题解报告

A - Acperience HDU - 5734 题意: 给你一个加权向量,需要我们找到一个二进制向量和一个比例因子α,使得|W-αB|的平方最小,而B的取值为+1,-1,我们首先可以想到α为输入数据的平均值,考虑到是平方和,然后化简表达式,可以得到一个化简的式子,用n通分,可以做到没有除法,然后分子分母化简到互质. #define _CRT_SECURE_NO_WARNINGS #include<cstdio> #include<cstring> #include<iom

Ohl&#224;l&#224;

Chap 1数数字 un 1 deux 2 trois 3 quatre 4 cinq 5 six 6 sept 7 huit 8 neuf 9 dix 10 Chap 2 讲地名 Paris 巴黎 Lyon 里昂 Bordeaux 波尔多 Marseille 马赛 Grenoble 格勒诺布尔 Lille 里尔 Orléans 奥尔良 Evian 依云 Chap 3 中国地名法语发音 Pékin 北京 Shanghai 上海 Canton 广州 Hong Kong 香港 Nankin 南京 C

2016 全国多校第二场 训练日志

solve 4 (131/582) A Acperience (解方程) E Eureka (几何点对相关) I It's All In The Mind (签到) K Keep On Movin (签到) L La Vie en rose (字符串hash) <qj> 思路: 正解可能是dp之类的,用hash字符串水了出来. 1.对于t串,hash成一个数字.  复杂度O(|t|) 2.对s进行一次滚动hash. 复杂度O(|s|) 3. 对于截取的一段ss,如果ss的hash值等于t的ha

关于 bitset 的一些题目

参考 http://www.cplusplus.com/reference/bitset/bitset/ https://blog.csdn.net/snowy_smile/article/details/79120063 bitset bitset<MAXN> b; b.any() // b中是否存在置为1的二进制位. b.none() // b中是否不存在置为1的二进制位. b.count() // b中置为1的二进制位的个数. b.size() // b中二进制位数的个数. b[pos]