第一问是裸DP。。。。
第二问还是裸DP,注意一种特殊情况(我忘了是什么特殊情况了QAQQQ)
滚动数组可以压掉一维,而且 ^ (xor)比 ! (not)慢。。。
1 /************************************************************** 2 Problem: 2423 3 User: rausen 4 Language: C++ 5 Result: Accepted 6 Time:964 ms 7 Memory:892 kb 8 ****************************************************************/ 9 10 #include <cstdio> 11 #include <cstring> 12 #include <algorithm> 13 14 using namespace std; 15 const int N = 5005; 16 const int mod = 100000000; 17 int f[2][N], g[2][N]; 18 int l1, l2, cur, i, j; 19 char s1[N], s2[N]; 20 21 void read(){ 22 char ch = getchar(); 23 while ((ch < 'A' || ch > 'Z') && ch != '.') 24 ch = getchar(); 25 while (ch != '.') 26 s1[++l1] = ch, ch = getchar(); 27 ch = getchar(); 28 while ((ch < 'A' || ch > 'Z') && ch != '.') 29 ch = getchar(); 30 while (ch != '.') 31 s2[++l2] = ch, ch = getchar(); 32 } 33 34 int main(){ 35 read(); 36 for (j = 0; j <= l2; ++j) 37 g[0][j] = 1; 38 g[1][0] = 1; 39 cur = 0; 40 for (i = 1; i <= l1; ++i) 41 for (j = 1, cur ^= 1; j <= l2; ++j) 42 if (s1[i] == s2[j]){ 43 f[cur][j] = f[!cur][j - 1] + 1; 44 g[cur][j] = g[!cur][j - 1]; 45 if (f[cur][j] == f[cur][j - 1]) 46 g[cur][j] += g[cur][j - 1], g[cur][j] %= mod; 47 if (f[cur][j] == f[!cur][j]) 48 g[cur][j] += g[!cur][j], g[cur][j] %= mod; 49 }else{ 50 f[cur][j] = f[!cur][j], g[cur][j] = g[!cur][j]; 51 if (f[cur][j] < f[cur][j - 1]){ 52 f[cur][j] = f[cur][j - 1]; 53 g[cur][j] = g[cur][j - 1]; 54 }else if (f[cur][j] == f[cur][j - 1]){ 55 g[cur][j] += g[cur][j - 1]; 56 if (f[!cur][j - 1] == f[cur][j]) 57 g[cur][j] -= g[!cur][j - 1]; 58 g[cur][j] %= mod; 59 } 60 } 61 printf("%d\n%d\n", f[cur][l2] % mod, g[cur][l2] % mod); 62 return 0; 63 }
By Xs酱~ 转载请说明
博客地址:http://www.cnblogs.com/rausen