码迷,mamicode.com
首页 > 其他好文 > 详细

UVaLive 6697 Homework Evaluation (DP)

时间:2016-08-24 01:00:06      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

题意:给出一个长字符串,再给一个短字符串,进行匹配,如果第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;
}

 

UVaLive 6697 Homework Evaluation (DP)

标签:

原文地址:http://www.cnblogs.com/dwtfukgv/p/5801244.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!