HDU5716, HDU5745【dp+bitset】

时间:2023-03-08 17:35:41
HDU5716, HDU5745【dp+bitset】

DP+bitset

 HDU5716

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

 #include <bits/stdc++.h>
#define X first
#define Y second
#define mp make_pair
#define pii pair<int, int>
#define gg puts("gg");
using namespace std;
const int N = 2e6+;
int id(char c){
if(c >= ''&& c <= '') return c-'';
if(c >= 'a'&& c <= 'z') return c-'a'+;
if(c >= 'A'&& c <= 'Z') return c-'A'+;
return -;
}
char s[N], t[];
bitset<> se[], dp;
int main(){
while(gets(s+)){
for(int i = ; i < ; i++) se[i].reset();
dp.reset();
int m, n; scanf("%d", &n);
for(int i = ; i <= n; i++){
scanf("%d", &m);
scanf(" %s", t);
for(int j = ; t[j]; j++)
se[ id(t[j]) ].set(i);
}
dp.set();
bool tag = true;
for(int i = ; s[i]; i++){
if(id(s[i]) >= )
dp = (dp<<)&se[ id(s[i]) ];
else
dp.reset();
dp.set();
if(dp.test(n))
tag = false, printf("%d\n", i-n+);
}
if(tag) puts("NULL");
getchar();
}
return ;
}

 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.

 #include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5+;
char s1[N], s2[];
bitset<N> se[], dp[];
int main(){
int t; scanf("%d", &t);
while(t--){
int n, m;
scanf("%d%d", &n, &m);
scanf("%s%s", s1+, s2+);
int l1 = strlen(s1+), l2 = strlen(s2+);
for(int i = ; i < ; i++){
se[i].reset();
for(int j = ; j <= l1; j++)
se[i][j] = (s1[j] == 'a'+i);
}
//se[i][j]: s1的第j个字符是不是i
dp[].set();
for(int i = ; i <= l2; i++){
dp[i%] = (dp[(i+)%]<<)&se[s2[i]-'a'];
if(i >= )
dp[i%] |= (dp[(i+)%]<<)&se[s2[i-]-'a']&(se[s2[i]-'a']<<);
dp[i%][] = ;
}
for(int i = l2; i <= l1; i++)
printf("%d", dp[l2%][i] == );
for(int i = ; i < l2; i++) putchar('');
puts("");
}
return ;
}