BestCoder Round #47 1003

solution : 就按题解敲了一遍,好久没写这种dp

1 #include <cstdio>
 2 #include <cstring>
 3 #include <string>
 4 #include <vector>
 5 #include <algorithm>
 6 #include <iostream>
 7 using namespace std;
 8 typedef long long LL;
 9 const int MAX = 1e3+10;
10 const int MOD = 1e9+7;
11 int dp[MAX][MAX];
12 LL f[MAX][MAX];
13 int Next[30];
14 char a[MAX],b[MAX];
15 int main() {
16     int cas;
17     scanf("%d",&cas);
18     while(cas--) {
19         scanf("%s %s",a+1,b+1);
20         memset(dp,0,sizeof(dp));
21         int n=strlen(a+1);
22         int m=strlen(b+1);
23         for(int i=1;i<=n;i++) {
24             for(int j=1;j<=m;j++) {
25                 if(a[i]==b[j]) {
26                     dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1);
27                 }
28                 else {
29                     dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
30                 }
31             }
32         }
33         memset(f,0,sizeof(f));
34         for(int i=0;i<=n;i++) {
35             memset(Next,0,sizeof(Next));
36             for(int j=0;j<=m;j++) {
37                 if(dp[i][j]==0) {
38                     f[i][j]=1;
39                 }
40                 else {
41                     Next[b[j]-‘a‘]=j;
42                     if(dp[i-1][j]==dp[i][j]) {
43                         f[i][j]=(f[i][j]+f[i-1][j])%MOD;
44                     }
45                     if(Next[a[i]-‘a‘]) {
46                         int p=Next[a[i]-‘a‘];
47                         if(p&&dp[i-1][p-1]+1==dp[i][j]) {
48                             f[i][j]=(f[i][j]+f[i-1][p-1])%MOD;
49                         }
50                     }
51                 }
52             }
53         }
54         cout<<f[n][m]<<endl;
55     }

56 }

时间: 2024-08-01 08:58:29

BestCoder Round #47 1003的相关文章

从lca到树链剖分 bestcoder round#45 1003

bestcoder round#45 1003 题,给定两个点,要我们求这两个点的树上路径所经过的点的权值是否出现过奇数次.如果是一般人,那么就是用lca求树上路径,然后判断是否出现过奇数次(用异或),高手就不这么做了,直接树链剖分.为什么不能用lca,因为如果有树退化成链,那么每次询问的复杂度是O(n), 那么q次询问的时间复杂度是O(qn) 什么是树链剖分呢? 就是把树的边分成轻链和重链 http://blogsina.com.cn/s/blog_6974c8b20100zc61.htmlh

BestCoder Round #29 1003 (hdu 5172) GTY&#39;s gay friends [线段树 判不同 预处理 好题]

传送门 GTY's gay friends Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 264    Accepted Submission(s): 57 Problem Description GTY has n gay friends. To manage them conveniently, every morning he o

HDU 5280 BestCoder Round #47 1001:Senior&#39;s Array

Senior's Array Accepts: 199 Submissions: 944 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 某天学姐姐得到了一个数组A,在这个数组的所有非空区间中,她找出了一个区间和最大的,并把这个区间和定义为这个数组的美丽值. 但是她觉得这个数组不够美,于是决定修理一下这个数组. 学姐姐将会进行一次操作,把原数组中的某个数修改为P(必须修改)

HDU 5281 BestCoder Round #47 1002:Senior&#39;s Gun

Senior's Gun Accepts: 235 Submissions: 977 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 学姐姐是一个酷酷的枪手. 她常常会随身携带n把枪,每把枪有一个攻击力a[i]. 有一天她遇到了m只怪兽,每只怪兽有一个防御力b[j].现在她决定用手中的枪消灭这些怪兽. 学姐姐可以用第i把枪消灭第j只怪兽当且仅当b[j]≤a[i],同时她会获

HDU 5806 NanoApe Loves Sequence Ⅱ(尺取+思维)——BestCoder Round #86 1003

传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 514    Accepted Submission(s): 248 Problem Description NanoApe, the Retired Dog, has returned back to prepare for f

HDU 5778 abs(暴力枚举)——BestCoder Round #85 1003

传送门 abs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1474    Accepted Submission(s): 511 Problem Description Given a number x, ask positive integer y≥2, that satisfy the following condition

HDU 5682/BestCoder Round #83 1003 zxa and leaf 二分+树

zxa and leaf Problem Description zxa have an unrooted tree with n nodes, including (n−1) undirected edges, whose nodes are numbered from 1 to n. The degree of each node is defined as the number of the edges connected to it, and each node whose degree

BestCoder Round#11div2 1003

----- 有时候如果枚举起点超时,那么试试枚举终点. 枚举每一个i为终点(0<= i < n),且维护起点下标startPos 对于终点i,cnt[str[i]] ++,   如果小于等于k的话,那么以i为结尾的符合条件的字串的个数为i-startPos+1 如果大于k的话,改变区间下标startPos, 保证区间startPos-->i内相同字母的个数不超过k 1 while(true) 2 { 3 cnt[str[startPos]] --; 4 if(str[startPos]

BestCoder Round#8 1003

dp[i][j] 表示以i结尾的长度为j的递增子序列dp[i][j] = sum(dp[k][j])     k<i && a[i] >a[j]如果只是单纯的循环for(j=2; j<=m; ++j) for(i=1; i<=n; ++i) for(k=1; k<i; ++k) if(a[i] > a[j]) dp[i][j] += dp[k][j-1];时间复杂度是O(n * n * m)   TLE但是k循环可以用树状数组来优化,k循环可以看做是一个