• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
dwtfukgv
博客园    首页    新随笔    联系   管理    订阅  订阅
UVaLive 6697 Homework Evaluation (DP)

题意:给出一个长字符串,再给一个短字符串,进行匹配,如果第i个恰好匹配,则 +8,;如果不匹配,可以给长或短字符串添加-,先后匹配,这样-3,

连续的长字符串添加-,需要减去一个4;也可不给添加-,则-5。

析:dp[i][j][0] 表示第一个字符串第 i 个位置,和第二个字符串的第 j 个位置相匹配,dp[i][j][1] 表示第一个字符串第 i 个位置,和第二个字符串的第 j 个位置加_相匹配.

那么怎么转移呢?如果s1[i] == s2[j] 那么 dp[i+1][j+1][0] = Max(dp[i+1][j+1][0], Max(dp[i][j][0], dp[i][j][1])+8);意思就是匹配的加8分,

如果不相等,dp[i+1][j+1][0] = Max(dp[i+1][j+1][0], Max(dp[i][j][0], dp[i][j][1])-5);不匹配,减 5 呗。

dp[i][j+1][1] = Max(dp[i][j+1][1], Max(dp[i][j][0]-7, dp[i][j][1]-3));
dp[i+1][j][1] = Max(dp[i+1][j][1], Max(dp[i][j][0]-7, dp[i][j][1]-3));

同样的意思。注意,这个题说学生的串不过50,但是你开小于100就会WA。。。。真坑啊

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e6 + 5;
const int mod = 1e8;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, -1, 1, 1, -1};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
char s1[110], s2[110];
int dp[110][110][2];

int main(){
    int T;  cin >> T;
    while(T--){
        scanf("%s", s1);  scanf("%s", s2);
        int len1 = strlen(s1);
        int len2 = strlen(s2);
        for(int i = 0; i <= len1; ++i)
            for(int j = 0; j <= len2; ++j)
                for(int k = 0; k < 2; ++k)
                    dp[i][j][k] = -INF;

        for(int i = 0; i <= len1; ++i)  dp[i][0][0] = 0;
        for(int i = 0; i < len1; ++i){
            for(int j = 0; j < len2; ++j){
                if(s1[i] == s2[j])  dp[i+1][j+1][0] = Max(dp[i+1][j+1][0], Max(dp[i][j][0], dp[i][j][1])+8);
                else dp[i+1][j+1][0] = Max(dp[i+1][j+1][0], Max(dp[i][j][0], dp[i][j][1])-5);

                dp[i][j+1][1] = Max(dp[i][j+1][1], Max(dp[i][j][0]-7, dp[i][j][1]-3));
                dp[i+1][j][1] = Max(dp[i+1][j][1], Max(dp[i][j][0]-7, dp[i][j][1]-3));
            }
        }

        int ans = -INF;
        for(int i = 0; i <= len1; ++i)
            ans = Max(ans, Max(dp[i][len2][0], dp[i][len2][1]));
        printf("%d\n", ans);
    }
    return 0;
}

 

posted on 2016-08-23 23:27  dwtfukgv  阅读(320)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3