BestCoder Round #47 1003
2015-07-12 11:46
381 查看
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;
const int MOD = 1e9+7;
int dp[MAX][MAX];
LL f[MAX][MAX];
int Next[30];
char a[MAX],b[MAX];
int main() {
int cas;
scanf("%d",&cas);
while(cas--) {
scanf("%s %s",a+1,b+1);
memset(dp,0,sizeof(dp));
int n=strlen(a+1);
int m=strlen(b+1);
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
if(a[i]==b[j]) {
dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1);
}
else {
dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
}
}
}
memset(f,0,sizeof(f));
for(int i=0;i<=n;i++) {
memset(Next,0,sizeof(Next));
for(int j=0;j<=m;j++) {
if(dp[i][j]==0) {
f[i][j]=1;
}
else {
Next[b[j]-'a']=j;
if(dp[i-1][j]==dp[i][j]) {
f[i][j]=(f[i][j]+f[i-1][j])%MOD;
}
if(Next[a[i]-'a']) {
int p=Next[a[i]-'a'];
if(p&&dp[i-1][p-1]+1==dp[i][j]) {
f[i][j]=(f[i][j]+f[i-1][p-1])%MOD;
}
}
}
}
}
cout<<f
[m]<<endl;
}56 }
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;
const int MOD = 1e9+7;
int dp[MAX][MAX];
LL f[MAX][MAX];
int Next[30];
char a[MAX],b[MAX];
int main() {
int cas;
scanf("%d",&cas);
while(cas--) {
scanf("%s %s",a+1,b+1);
memset(dp,0,sizeof(dp));
int n=strlen(a+1);
int m=strlen(b+1);
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
if(a[i]==b[j]) {
dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1);
}
else {
dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
}
}
}
memset(f,0,sizeof(f));
for(int i=0;i<=n;i++) {
memset(Next,0,sizeof(Next));
for(int j=0;j<=m;j++) {
if(dp[i][j]==0) {
f[i][j]=1;
}
else {
Next[b[j]-'a']=j;
if(dp[i-1][j]==dp[i][j]) {
f[i][j]=(f[i][j]+f[i-1][j])%MOD;
}
if(Next[a[i]-'a']) {
int p=Next[a[i]-'a'];
if(p&&dp[i-1][p-1]+1==dp[i][j]) {
f[i][j]=(f[i][j]+f[i-1][p-1])%MOD;
}
}
}
}
}
cout<<f
[m]<<endl;
}56 }
相关文章推荐
- 基于UML和ASP.NET实现三层B/S结构系统开发
- 报错:1130-host ... is not allowed to connect to this MySql server
- HDU 2048 神、上帝以及老天爷
- iOS和Android的对比
- 黑马程序员——JAVA基础---类与对象
- 对第14章类型信息中RegisteredFactories.java的理解
- openldap 操作实战
- POJ 3126 && HDU 1973 Prime Path(bfs)
- CENTOS6.5中VIM插件配置,重点记录YouCompleteMe的安装 2015-7
- 关于PHP会话:session和cookie
- 人群拥挤检测之人数检测二
- 卓越程序员密码
- .Net路(十三)导出数据库到EXCEL
- 笔试题 百度2015大数据云计算研发笔试(关灯开灯)
- C++拷贝构造函数详解
- HTTP协议详解(真的很经典)
- 字符串hash
- leetcode 62:Unique Paths
- TCP和http的keepalive
- 一个完整的C++程序SpreadSheet - 1) 类的声明和定义