[FJOI2016]所有公共子序列问题-解题报告
n,m不要打反
不错,自己搞出来了
看题解都是无法滚动数组的压位高精,给一个可以滚动数组的DP。
首先,这道题有一个很显然的二维
但是加上高精度之后,这道题会炸空间,怎么办?(压位
我们改进一下状态,
那么转移有两种,我们还是必须贪心地匹配,才能保证只计数一次:
- 如果
a[i]=b[j] :
枚举下一个是什么。
- 否则:
忽略掉i即可。
为什么这个做法更优秀呢?因为这个做法可以扩展到LCIS计数且字符集很大且卡空间的情况。
由于没有压位导致常数很大的代码:
#define N 3015
il int ha(const char c)
{
if(c<='Z') return c-'A';
return c-'a'+26;
}
struct XL
{
char s[N];
int n,nxt[N][52];
void init()
{
scanf("%s",s+1); n=strlen(s+1);
for(ri i=n-1; i>=0; --i)
{
memcpy(nxt[i],nxt[i+1],sizeof nxt[i]);
nxt[i][ha(s[i+1])]=i+1;
}
}
} A,B;
int typ;
int cnt;
int d[N],dep;
void dfs(int x,int y)
{
if(typ)
{
for(ri i=1; i<=dep; ++i) putchar(d[i]<26?d[i]+'A':d[i]-26+'a');
enter;
}
++cnt;
for(ri i=0; i<52; ++i) if(A.nxt[x][i]&&B.nxt[y][i])
{
d[++dep]=i;
dfs(A.nxt[x][i],B.nxt[y][i]);
--dep;
}
}
#define MXLEN 200
struct Bigint
{
int num[MXLEN],sz;
Bigint(int x = 0)
{
mem(num,0),sz=0;
while(x) num[++sz]=x%10,x/=10;
}
int &operator[](const int x) {return num[x];}
const int &operator[](const int x) const {return num[x];}
void operator*=(const Bigint &x)
{
static int tmp[MXLEN]; clr(tmp+1,tmp+1+sz+x.sz);
for (ri i=1; i<=sz; ++i)
for(ri j=1; j<=x.sz; ++j) tmp[i+j-1]+=num[i]*x[j];
sz+=x.sz;
for(ri i=1; i<=sz; ++i)
{
tmp[i+1]+=tmp[i]/10;
tmp[i]%=10;
}
while(sz&&tmp[sz]==0) --sz;
for(ri i=1; i<=sz; ++i) num[i]=tmp[i];
}
Bigint operator*(const Bigint &x) const
{
Bigint res=*this; res*=x;
return res;
}
void operator*=(const int x) // positive
{
for(ri i=1; i<=sz; ++i) num[i]*=x;
sz+=10;
for(ri i=1; i<=sz; ++i)
{
num[i+1]+=num[i]/10;
num[i]%=10;
}
while(sz&&num[sz]==0) --sz;
}
Bigint operator*(const int x) const
{
Bigint res=*this; res*=x;
return res;
}
void operator+=(const Bigint &x)
{
for (ri i=1,t; i<=x.sz; ++i)
{
t=num[i]+x[i];
num[i]=t%10;
num[i+1]+=t/10;
}
sz=max(sz,x.sz); while(num[sz+1]) ++sz;
}
Bigint operator+(const Bigint &x) const
{
Bigint res=*this; res+=x;
return res;
}
void read()
{
static char tc[MXLEN];
scanf("%s", tc+1); sz=strlen(tc+1);
for(ri i=1; i<=sz; ++i) num[i]=tc[sz-i+1]-'0';
}
void print() const
{
int t=max(sz,1);
for(ri i=t; i>=1; --i) putchar('0'+num[i]); enter;
}
};
int n,m;
Bigint _dp[2][N],ans;
void solve()
{
auto f=_dp[0],g=_dp[1];
for(ri i=n; i>=1; --i)
{
swap(f,g); memset(f,0,sizeof _dp[0]);
for(ri j=m; j>=1; --j)
if(A.s[i]==B.s[j])
{
f[j]+=1;
for(ri k=0; k<52; ++k)
if(B.nxt[j][k]) f[j]+=g[B.nxt[j][k]];
}
else f[j]=g[j];
}
ans+=1;
for(ri k=0; k<52; ++k)
if(B.nxt[0][k]) ans+=f[B.nxt[0][k]];
ans.print();
}
signed main()
{
#ifdef M207
freopen("in.in","r",stdin);
// freopen("ot.out","w",stdout);
#endif
in(n,m);
A.init(),B.init();
in(typ);
if(typ) {dfs(0,0); out(cnt);}
else solve();
return 0;
}