HDU5716, HDU5745【dp+bitset】

DP+bitset

 HDU5716

dp[i][j] = dp[i-1][j-1] && (s[i] in set[j]); 第二维压bitset

 1 #include <bits/stdc++.h>
 2 #define X first
 3 #define Y second
 4 #define mp make_pair
 5 #define pii pair<int, int>
 6 #define gg puts("gg");
 7 using namespace std;
 8 const int N = 2e6+5;
 9 int id(char c){
10     if(c >= '0'&& c <= '9') return c-'0';
11     if(c >= 'a'&& c <= 'z') return c-'a'+10;
12     if(c >= 'A'&& c <= 'Z') return c-'A'+36;
13     return -1;
14 }
15 char s[N], t[70];
16 bitset<505> se[70], dp;
17 int main(){
18     while(gets(s+1)){
19         for(int i = 0; i < 70; i++) se[i].reset();
20         dp.reset();
21         int m, n; scanf("%d", &n);
22         for(int i = 1; i <= n; i++){
23             scanf("%d", &m);
24             scanf(" %s", t);
25             for(int j = 0; t[j]; j++)
26                 se[ id(t[j]) ].set(i);
27         }
28         dp.set(0);
29         bool tag = true;
30         for(int i = 1; s[i]; i++){
31             if(id(s[i]) >= 0)
32                 dp = (dp<<1)&se[ id(s[i]) ];
33             else
34                 dp.reset();
35             dp.set(0);
36             if(dp.test(n))
37                 tag = false, printf("%d\n", i-n+1);
38         }
39         if(tag) puts("NULL");
40         getchar();
41     }
42     return 0;
43 }
View Code

 

 HDU5745


dp[i][j] = (dp[i-1][j-1]&&s1[i] == s2[j])|(dp[i-2][j-2]&&s1[i] == s2[j-1]&&s1[i-1] == s2[j])

压小的一维T了,压大的一维可以AC.

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N = 1e5+5;
 5 char s1[N], s2[5111];
 6 bitset<N> se[26], dp[3];
 7 int main(){
 8     int t; scanf("%d", &t);
 9     while(t--){
10         int n, m;
11         scanf("%d%d", &n, &m);
12         scanf("%s%s", s1+1, s2+1);
13         int l1 = strlen(s1+1), l2 = strlen(s2+1);
14         for(int i = 0; i < 26; i++){
15             se[i].reset();
16             for(int j = 1; j <= l1; j++)
17                 se[i][j] = (s1[j] == 'a'+i);
18         }
19         //se[i][j]: s1的第j个字符是不是i
20         dp[0].set();
21         for(int i = 1; i <= l2; i++){
22             dp[i%3] = (dp[(i+2)%3]<<1)&se[s2[i]-'a'];
23             if(i >= 2)
24                 dp[i%3] |= (dp[(i+1)%3]<<2)&se[s2[i-1]-'a']&(se[s2[i]-'a']<<1);
25             dp[i%3][0] = 1;
26         }
27         for(int i = l2; i <= l1; i++)
28             printf("%d", dp[l2%3][i] == 1);
29         for(int i = 1; i < l2; i++) putchar('0');
30         puts("");
31     }
32     return 0;
33 }
View Code

 

posted @ 2016-10-25 19:41  我在地狱  阅读(225)  评论(0编辑  收藏  举报