dp+bitset优化______La Vie en rose( hdu 5745 2016多校第二场)

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





题意:
有串s,p.串p可以经过一些变换,变换规则是,每个字符可以跟它前一个字符或者后一个字符交换位置。交换次数不限,但是每个字符只能交换或者被交换一次。

结果输出一个n位的二进制,对于匹配串的字符位置i,如果s的子串(si,si+1,si+2,...,si+m-1)可以由p串变换得出的话,则这个位置输出“1”,否则输出“0”。


分析:

这个题目很坑,卡了常数,姿势不好就会超时。


首先很容易可以看出来这是一个dp题。dp[ i ][ k ][ j ]  表示 s[ i ] 经过 k 操作 能够跟 p[ j ] 匹配上并且 p[ j ] 之前的字符也已经匹配上了。

k  = 0 ,当前字符跟之前的字符交换。

k = 1 ,当前字符不交换

k = 2 ,当前字符跟之后的字符交换。

那么状态转移为:

dp[ j ][ 0 ][ i ] = dp[ j-1 ][ 2 ][ i-1 ] && s[ i ] == p[ j-1 ]
dp[ j ][ 1 ][ i ] = ( dp[ j-1 ][ 1 ][ i-1 ] || dp[ j-1 ][ 0 ][ i-1 ]) && s[ i ] == p[ j ]
dp[ j ][ 2 ][ i ] = ( dp[ j-1 ][ 1 ][ i-1 ] || dp[ j-1 ][ 0 ][ i-1 ]) && s[ i ] == p[ j+1 ]


好我们根据这个状态转移方程式可以写出第一份代码:


代码1:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N=1e5+10;
const int M=5010;
char s[N],p[M];
bool dp[M][3][N];

int main()
{
    std::cout.sync_with_stdio(false);
    int t,n,m;
    cin >> t;
    while(t--)
    {
        cin >> n >> m;
        cin >> s >> p;

        memset(dp,0,sizeof(dp));

        for(int i = 0 ; i < n ; i ++)
        {
            if(s[i] == p[0])dp[0][1][i] = 1;
            if(m > 1 && s[i] == p[1]) dp[0][2][i] = 1;
        }

        for(int j = 1 ; j < m ; j ++)
        {
            for(int i = 0; i < n ; i ++)
            {
                dp[j][0][i] = dp[j-1][2][i-1]&&(s[i] == p[j-1]);
                dp[j][1][i] = (dp[j-1][0][i-1] || dp[j-1][1][i-1]) && (s[i] == p[j]);
                if(j+1<m) dp[j][2][i] = (dp[j-1][1][i-1] || dp[j-1][0][i-1]) && (s[i] == p[j+1]);
            }
        }
        for(int i = 0 ; i < n ; i ++)
        {
            if(dp[m-1][0][i+m-1] || dp[m-1][1][i+m-1]) cout << "1";
            else cout << "0";
        }
        cout << endl;
    }
    return 0;
}



然而显然以上代码的时间复杂度是O(m*n) 空间复杂度是O(m*n)。先不说时间,根据m,n的范围可以知道dp[M][3][N]肯定会爆。我们仔细观察可以发现dp的递推只跟之前一个状态有关。所以我们可以使用滚动数组优化空间,使得空间复杂度变成O(n).


代码2:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N=1e5+10;
const int M=5010;
char s[N],p[M];
bool dp[2][3][N];

int main()
{
    std::cout.sync_with_stdio(false);
    int t,n,m;
    cin >> t;
    while(t--)
    {
        cin >> n >> m;
        cin >> s >> p;
        memset(dp,0,sizeof(dp));
        int cur = 0;
        for(int i = 0 ; i < n ; i ++)
        {
            if(s[i] == p[0])dp[cur][1][i] = 1;
            if(m > 1 && s[i] == p[1]) dp[cur][2][i] = 1;
        }
        for(int j = 1 ; j < m ; j ++)
        {
            cur ^= 1;
            for(int i = 0; i < n ; i ++)
            {
                dp[cur][0][i] = dp[cur^1][2][i-1]&&(s[i] == p[j-1]);
                dp[cur][1][i] = (dp[cur^1][0][i-1] || dp[cur^1][1][i-1]) && (s[i] == p[j]);
                if(j+1<m) dp[cur][2][i] = (dp[cur^1][1][i-1] || dp[cur^1][0][i-1]) && (s[i] == p[j+1]);
            }
        }
        for(int i = 0 ; i < n ; i ++)
        {
            if(dp[cur][0][i+m-1] || dp[cur][1][i+m-1]) cout << "1";
            else cout << "0";
        }
        cout << endl;
    }
    return 0;
}

好,现在只剩时间了,其实这里我们需要使用一个奇计淫巧 ----------- bitset优化,

实际上根据算法大体上已经不能再优化了O(m*n).但是显然这个代码是会超时的(比赛的时候时间开的8秒。暴力可过,挂出来的题只有4秒)。

我们可以观察算法主体代码:

for(int j = 1 ; j < m ; j ++)
{
    for(int i = 0; i < n ; i ++)
    {
        dp[j][0][i] = dp[j-1][2][i-1]&&(s[i] == p[j-1]);
        dp[j][1][i] = (dp[j-1][0][i-1] || dp[j-1][1][i-1]) && (s[i] == p[j]);
        if(j+1<m) dp[j][2][i] = (dp[j-1][1][i-1] || dp[j-1][0][i-1]) && (s[i] == p[j+1]);
    }


内循环可以看作是每位(i)的两个数通过与、或等方式得到另一个数。注意这里强调是每一位都执行的相同操作。并且每位只有0或者1两个值。我们可以联想到位运算。

这个时候我们把dp[ j ][ k ]  看作一个bool型的数组。嗯也可以看做一个长度为N的一个二进制数。那么内层循环可以直接用位运算实现而不用循环。这样做的好处是什么呢?

它可以把时间复杂度从O(n*m) 压缩到 O(n*m/w) w是电脑的机器字长。

这里我们使用的bool数组就是bitset容器。具体优化实现看这里------------bitset优化,


代码3(AC代码):

#include <cstdio>
#include <cstring>
#include <iostream>
#include<bitset>
using namespace std;
const int N=1e5+10;
const int M=5010;
char s[N],p[M];
bitset<N>dp[2][3];
bitset<N>w[30];

int main()
{
    std::cout.sync_with_stdio(false);
    int t,n,m,cur;
    cin >> t;
    while(t--)
    {
        cin >> n >> m;
        cin >> s >> p;

        for(int i=0;i<30;i++)
            w[i].reset();
        for(int i=0;i<n;i++)
            w[s[i]-'a'][i]=1;
        for(int i=0;i<2;i++)
            for(int j=0;j<3;j++)
                dp[i][j].reset();
        cur=1;
        dp[cur][1]=w[p[0]-'a'];
        if(m>1) dp[cur][2]=w[p[1]-'a'];
        for(int j=1;j<m;j++)
        {
            cur^=1;
            // 注意这里之所以要左移1位是因为w[p[j-1]-'a']每个i是跟dp[cur^1][2]的i-1相与。所以要将右边的左移,才能使得i-1与上i.
            dp[cur][0]=w[p[j-1]-'a']&(dp[cur^1][2]<<1); 
            dp[cur][1]=w[p[j]-'a']&((dp[cur^1][0]|dp[cur^1][1])<<1);
            if(j+1<m) dp[cur][2]=w[p[j+1]-'a']&((dp[cur^1][0]|dp[cur^1][1])<<1);
        }
        for(int i=0;i<n;i++)
            if(dp[cur][0][i+m-1]||dp[cur][1][i+m-1])
                printf("1");
            else
                printf("0");
        printf("\n");
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
bitset的to_string函数的作用是将位组成的数据集合转换成字符串。它可以将bitset对象中的位数转换成一个字符串,其中每个位对应字符串中的一个字符。例如,在C++中,通过使用to_string函数可以将一个由bit组成的集合1111转换成字符串"1111"。这个函数可以方便地将位集合的值以字符串的形式输出。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [算法之旅,直奔<bitset>之一 to_string](https://blog.csdn.net/cqs_2012/article/details/17498437)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [动态Bitset源代码](https://download.csdn.net/download/zhenwenhe/10004025)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [蓝桥杯day18_明码_bitset(),to_string()函数使用](https://blog.csdn.net/ratsquealer/article/details/115185236)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值