Description

Takahashi has decided to give a string to his mother.

The value of a string T is the length of the longest common subsequence of T and T', where T' is the string obtained by reversing T. That is, the value is the longest length of the following two strings that are equal: a subsequence of T (possibly non-contiguous), and a subsequence of T' (possibly non-contiguous).

Takahashi has a string S. He wants to give her mother a string of the highest possible value, so he would like to change at most K characters in S to any other characters in order to obtain a string of the highest possible value. Find the highest possible value achievable.

题目大意:在改变原串最多K个字母的前提下,使得T和T的反串的LCS尽量长

Solution

猜个结论:T与T的反串的LCS等于T的最长回文子序列的长度

那么就做完了,做一个区间DP: 设 \(f[i][j][k]\) 表示区间 \([i,j]\) 修改了 \(k\) 次的最长的回文子序列的长度

分修改和不修改两种转移即可

#include<bits/stdc++.h>
using namespace std;
const int N=305;
int f[N][N][N],n,K;char s[N];
int main(){
freopen("pp.in","r",stdin);
freopen("pp.out","w",stdout);
scanf("%s%d",s+1,&K);n=strlen(s+1);
for(int i=1;i<=n;i++)
for(int k=0;k<=K;k++)f[i][i][k]=1;
for(int len=2;len<=n;len++){
for(int i=1;i+len-1<=n;i++){
int j=i+len-1;
for(int k=0;k<=K;k++){
f[i][j][k]=max(f[i+1][j][k],f[i][j-1][k]);
if(s[i]==s[j])f[i][j][k]=max(f[i][j][k],f[i+1][j-1][k]+2);
if(k)f[i][j][k]=max(f[i][j][k],f[i+1][j-1][k-1]+2);
}
}
}
cout<<f[1][n][K]<<endl;
return 0;
}

AtCoder Grand Contest 021 D - Reversed LCS的更多相关文章

  1. AtCoder Grand Contest 021

    A - Digit Sum 2 Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement Find t ...

  2. AtCoder Grand Contest 021完整题解

    提示:如果公式挂了请多刷新几次,MathJex的公式渲染速度并不是那么理想. 总的来说,还是自己太弱了啊.只做了T1,还WA了两发.今天还有一场CodeForces,晚上0点qwq... 题解还是要好 ...

  3. Atcoder Grand Contest 021 F - Trinity(dp+NTT)

    Atcoder 题面传送门 & 洛谷题面传送门 首先我们考虑设 \(dp_{i,j}\) 表示对于一个 \(i\times j\) 的网格,其每行都至少有一个黑格的合法的三元组 \((A,B, ...

  4. AtCoder Grand Contest 021题解

    传送门 \(A\) 咕咕 ll n,res;bool fl; int main(){ scanf("%lld",&n),fl=1; while(n>9)res+=9, ...

  5. AtCoder Grand Contest 012

    AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...

  6. AtCoder Grand Contest 011

    AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\( ...

  7. AtCoder Grand Contest 031 简要题解

    AtCoder Grand Contest 031 Atcoder A - Colorful Subsequence description 求\(s\)中本质不同子序列的个数模\(10^9+7\). ...

  8. AtCoder Grand Contest 010

    AtCoder Grand Contest 010 A - Addition 翻译 黑板上写了\(n\)个正整数,每次会擦去两个奇偶性相同的数,然后把他们的和写会到黑板上,问最终能否只剩下一个数. 题 ...

  9. AtCoder Grand Contest 009

    AtCoder Grand Contest 009 A - Multiple Array 翻译 见洛谷 题解 从后往前考虑. #include<iostream> #include< ...

随机推荐

  1. 从0开始的LeetCode生活—001-Two Sum

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  2. centos 安装配置 mysql

    安装环境:CentOS7 64位 MINI版,安装MySQL5.7 1.配置YUM源 在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads/repo ...

  3. map的infowindow的show事件(ArcGIS API for JS)

  4. 2018年东北农业大学春季校赛-wyh的吃鸡

    BFS: 1. 从起点开始BFS,遇到X点则return: 2. vis[px][py][0]代表经过pxpy这点前还没有找到车: vis[px][py][1]代表经过pxpy这点前已经找到车: 3. ...

  5. MSSQL---extents

    一.MSSQLextent分两种: 1. Mixed extent:每个表或索引创建时,MSSQL并不给它分配一个extent,而是在mixed extnet内分配一个页,空间需求扩大时,再分配一个… ...

  6. 利用封装、继承对Java代码进行优化

    注:本文实例分别可以在oldcastle(未优化的代码)和newcastle(优化后的代码)中查看,网址见文末 城堡游戏: 城堡中有多个房间,用户通过输入north, south, east, wes ...

  7. Extensions in UWP Community Toolkit - Overview

    概述 UWP Community Toolkit  中有一个 Extensions 的集合,它们可以帮助开发者实现很多基础功能,省去自己造轮子的过程,本篇我们先来看一下 Extensions 的功能都 ...

  8. angular2 学习笔记 の 移动端开发 ( 手势 )

    更新 : 2018-01-31 (hammer 的坑) hammer 的 pinch 在某种情况下会自动触发 panEnd,很奇葩. 解决方法就是记入时间呗 refer : https://githu ...

  9. GIT入门笔记(13)- GUI GIT

  10. vmvare入门(1)使用移动,不要使用复制

    1.复制虚拟机会产生新的自动网卡,原来的 System Eth0废了? 2.xftp链接的时候,要选择sftp方式连接,utf8编码.