Codeforces Round #358 (Div. 2) D. Alyona and Strings(DP)

题目链接:点击打开链接

思路:

类似于LCS, 只需用d[i][j][k][p]表示当前到了s1[i]和s2[j], 形成了k个子序列, 当前是否和上一个字符和上一个字符相连形成一个序列的最长序列和。

细节参见代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef long double ld;
const double eps = 1e-6;
const double PI = acos(-1);
const int mod = 1000000000 + 7;
const int INF = 0x3f3f3f3f;
// & 0x7FFFFFFF
const int seed = 131;
const ll INF64 = ll(1e18);
const int maxn = 1000 + 10;
int T,n,m, d[maxn][maxn][12][2], vis[maxn][maxn][12][2], kase = 0, k;
inline bool vail(int i, int j) {
    if(i < n && j < m) return true;
    else return false;
}
char s1[maxn], s2[maxn];
int dp(int i, int j, int s, int p) {
    int& ans = d[i][j][s][p];
    if(s > k) return -INF;
    if(i >= n || j >= m) {
        if(s == k) return 0;
        else return -INF;
    }
    if(vis[i][j][s][p] == kase) return ans;
    vis[i][j][s][p] = kase;
    ans = 0;
    if(p) {
        if(s1[i] == s2[j]) ans = max(ans, dp(i+1, j+1, s, p) + 1);
        if(s1[i] == s2[j]) ans = max(ans, dp(i+1, j+1, s+1, p) + 1);
        ans = max(ans, dp(i+1, j+1, s, p^1));

        ans = max(ans, dp(i, j+1, s, p^1));
        ans = max(ans, dp(i+1, j, s, p^1));
    }
    else {
        if(s1[i] == s2[j]) ans = max(ans, dp(i+1, j+1, s+1, p^1) + 1);
        ans = max(ans, dp(i+1, j+1, s, p));
        ans = max(ans, dp(i, j+1, s, p));
        ans = max(ans, dp(i+1, j, s, p));
    }
    return ans;
}
int main() {
    scanf("%d%d%d",&n,&m,&k);
    ++kase;
    scanf("%s%s", s1, s2);
    int ans = dp(0, 0, 0, 0);
    printf("%d\n", ans);
    return 0;
}
时间: 2024-10-13 00:25:35

Codeforces Round #358 (Div. 2) D. Alyona and Strings(DP)的相关文章

Codeforces Round #267 (Div. 2) C. George and Job(DP)补题

Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, he didn't have enough money, so George was going to work as a programmer. Now he faced the follow

Codeforces Round #401 (Div. 2)C. Alyona and Spreadsheet(暴力)

传送门 Description During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables. Now she has a table filled with integers. The table consists of n rows and m columns. By ai, j we will denote the in

Codeforces Round #107 (Div. 1) B. Quantity of Strings(推算)

http://codeforces.com/problemset/problem/150/B 题意: 给出n,m,k,n表示字符串的长度为n,m表示字符种类个数,k表示每k个数都必须是回文串,求满足要求的不同字符串有多少种. 思路:分奇偶推一下,当k为偶数时,容易发现如果n=k,那么有最多有k/2种不同的字符可填,如果n>k,你会发现此时所有位置都必须一样. 奇数的话会稍微麻烦一点,如果n=k,那么最多有k/2+1种不同的字符可填,如果n>k,你会发现此时最后只有2中不同的字符可填. 1 #i

Codeforces Round #267 (Div. 2) C. George and Job (dp)

wa哭了,,t哭了,,还是看了题解... 8170436                 2014-10-11 06:41:51     njczy2010     C - George and Job             GNU C++     Accepted 109 ms 196172 KB 8170430                 2014-10-11 06:39:47     njczy2010     C - George and Job             GNU C

[Codeforces Round #261 (Div. 2) E]Pashmak and Graph(Dp)

Description Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem. You are given a w

Codeforces Round #FF (Div. 2) C - DZY Loves Sequences (DP)

DZY has a sequence a, consisting of n integers. We'll call a sequence ai,?ai?+?1,?...,?aj (1?≤?i?≤?j?≤?n) a subsegment of the sequence a. The value (j?-?i?+?1) denotes the length of the subsegment. Your task is to find the longest subsegment of a, su

Codeforces Round #261 (Div. 2)459A. Pashmak and Garden(数学题)

题目链接:http://codeforces.com/problemset/problem/459/A A. Pashmak and Garden time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Pashmak has fallen in love with an attractive girl called Parmida s

Codeforces Round #258 (Div. 2) A. Game With Sticks(数学题)

题目链接:http://codeforces.com/contest/451/problem/A ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943/ma

Codeforces Round #216 (Div. 2) E. Valera and Queries (BIT)

题目大意: 给出很多条分布在 x 轴上的线段. 然后给出很多点集,问这些点集分布在多少条不同的线段上. 思路分析: 把点集分散成若干条线段. 如果点集做出的线段包含了某一条给出的线段的话,也就是说这个点集上不会有点在这条线段上. 所以我们就是求出 点集做出的线段包含了多少个给出的线段就可以了. 那么也就是比较l r的大小,排序之后用BIT #include <cstdio> #include <iostream> #include <algorithm> #includ