HDU
来源:互联网 发布:微信订单管理系统源码 编辑:程序博客网 时间:2024/05/17 03:09
简单 dp 稍微推一下就出来了
可以画个n * m 的矩阵 手动推一下 a [ i ] == b [ j ] 和 a [ i ] != b [ j ] 时的关系递推关系式
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <set>#include <queue>#include <algorithm>#include <vector>#include <cctype>#include <stack>#include <sstream>#include <list>#include <map>#include <assert.h>#define debug() puts("************")#define MS(a,b) memset(a,b,sizeof a)using namespace std;typedef long long ll;typedef pair<int,int> P;const int INF = 0x3f3f3f3f;const double inf = 1e20;const double eps = 1e-15;const int dx[] = {-1,0,1,0};const int dy[] = {0,1,0,-1};const int maxn = 1000 + 7, maxd = 100000 + 7;const ll mod = 1e9+7;int n, m;int a[maxn], b[maxn];ll dp[maxn][maxn];void init() { for(int i = 1; i <= n; ++i) scanf("%d", &a[i]); for(int i = 1; i <= m; ++i) scanf("%d", &b[i]); memset(dp, 0, sizeof dp);}void solve() { for(int i = 1; i <= n; ++i) { for(int j = 1; j <= m; ++j) { if(a[i] == b[j]) dp[i][j] = (dp[i][j-1] + dp[i-1][j] + 1) % mod; else dp[i][j] = (dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1] + mod) % mod; } }}int main() { while(scanf("%d %d", &n, &m) != EOF && n+m) { init(); solve(); printf("%lld\n", dp[n][m] % mod); } return 0;}
阅读全文
1 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- 上传本地代码到github
- 算法 二分查找的变种以及注意事项
- 矩阵归一化
- struts2 基本流程与配置
- BZOJ1009: [HNOI2008]GT考试(KMP+矩阵优化DP)
- HDU
- HTTP协议简要介绍
- 数据库中表的连接(多关系连接查询)
- 一些前端题目
- js中,方法没有完成,不能使用window.reload()
- 输出前k大的数
- js正则表达式学习之
- Oracle index
- HDU 2825 Wireless Password(AC自动机+状态压缩DP)