标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
2016 Multi-University Training Contest 5
思路:if(a[i]==b[t])
dp[i][t]=((1+dp[i][t-1]+dp[i-1][t])%mod+mod)%mod;
else
dp[i][t]=((dp[i][t-1]+dp[i-1][t]-dp[i-1][t-1])%mod+mod)%mod;
dp[i][t]表示答案,A数组的i位置,b数组的t位置;
利用前缀减少时间复杂度,从前面继承;
ps:又忘了取模的坑点,减法需要+mod%mod;
#include<bits/stdc++.h> using namespace std; #define ll __int64 #define esp 0.00000000001 const int N=1e3+10,M=1e7+10,inf=1e9+10,mod=1000000007; int a[N]; int b[N]; ll dp[N][N]; int main() { int x,y,z,i,t; while(~scanf("%d%d",&x,&y)) { memset(dp,0,sizeof(dp)); for(i=1;i<=x;i++) scanf("%d",&a[i]); for(i=1;i<=y;i++) scanf("%d",&b[i]); for(i=1;i<=x;i++) { for(t=1;t<=y;t++) if(a[i]==b[t]) dp[i][t]=((1+dp[i][t-1]+dp[i-1][t])%mod+mod)%mod; else dp[i][t]=((dp[i][t-1]+dp[i-1][t]-dp[i-1][t-1])%mod+mod)%mod; } printf("%I64d\n",dp[x][y]); } return 0; }
标签:
原文地址:http://www.cnblogs.com/jhz033/p/5734362.html