1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| #include<cstdio> #include<cctype> #include<cstring> using namespace std; const int N=3010,C=26,S=C<<1,D=20,M=1e9; int m,n,k,a[N+1],b[N+1],nexa[N+1][S],nexb[N+1][S],top; char x[N+2],y[N+2],res[N+2]; inline int max(int x,int y) { return x>=y?x:y; } struct number { int dig; long long *s; bool vis; void init(long long val=0) { dig=0,vis=true; s=new long long[D]; for(int i=0;i<D;++i) { s[i]=0; } if(val) { s[0]=val; } return; } void operator+=(const number ano) { dig=max(dig,ano.dig)+1; for(int i=0;i<=dig;++i) { s[i]+=ano.s[i]; s[i+1]+=s[i]/M,s[i]%=M; } while(dig&&!s[dig]) { --dig; } return; } }; number f[N+1][N+1]; void build(int n,int x[],int nex[][S]) { for(int i=n;i;--i) { memcpy(nex[i-1],nex[i],sizeof(nex[i])); nex[i-1][x[i]]=i; } return; } void dfs1(int x,int y) { puts(res+1); for(int i=0;i<S;++i) { if(nexa[x][i]&&nexb[y][i]) { res[++top]=i<C?i+'A':i+'a'-26; dfs1(nexa[x][i],nexb[y][i]); res[top--]='\0'; } } return; } void dfs2(int x,int y) { if(f[x][y].vis) { return; } f[x][y].init(1); for(int i=0;i<S;++i) { if(nexa[x][i]&&nexb[y][i]) { dfs2(nexa[x][i],nexb[y][i]); f[x][y]+=f[nexa[x][i]][nexb[y][i]]; } } return; } void print(number x) { printf("%lld",x.s[x.dig]); for(int i=x.dig-1;~i;--i) { printf("%09lld",x.s[i]); } putchar('\n'); return; } int main() { scanf("%d%d",&m,&n); scanf("%s%s",x+1,y+1); scanf("%d",&k); for(int i=1;i<=m;++i) { a[i]=isupper(x[i])?x[i]-'A':x[i]-'a'+26; } for(int i=1;i<=n;++i) { b[i]=isupper(y[i])?y[i]-'A':y[i]-'a'+26; } build(m,a,nexa); build(n,b,nexb); if(k) { dfs1(0,0); } dfs2(0,0); print(f[0][0]); return 0; }
|