http://codeforces.com/contest/814/problem/C

12
ooyomioomioo
2
1 o
2 o

这题我是用dp解的,不过好像很慢,比赛的时候算了下不会mle,就没滚动数组了。

dp[i][k][ch]表示以第i位结尾,允许变化k次,所求的字符是ch时的最大连续数量。

如果k > 0,那么dp[i][k][ch] > 0的,因为肯定可以把第i位变了。

那么对于第i位来说,如果str[i]和ch相同,那么应该是dp[i][k][ch] = dp[i - 1][k][ch] + 1,就是和上一段可以结合。而且不用花费变化次数,如果不同,那么需要把str[i]变成ch,才能和前面那一段结合,就是dp[i][k][ch] = dp[i - 1][k - 1][ch] + 1

复杂度n^2 * 26

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
char str[];
int lenstr;
//void dfs(char ch, int cur, int nowLen, int did, bool can) {
// if (did > lenstr) return;
// dp[ch][did] = max(dp[ch][did], nowLen);
// if (dp[ch][did] > nowLen && nowLen && can) return;
// if (cur > lenstr) return;
// if (str[cur] == ch) {
// dfs(ch, cur + 1, nowLen + 1, did, false);
// } else {
// dfs(ch, cur + 1, 0, did, false);
// dfs(ch, cur + 1, nowLen + 1, did + 1, true);
//
// }
//}
int dp[ + ][ + ][ + ];
int ans[ + ][ + ];
void work() {
int n;
scanf("%d", &n);
scanf("%s", str + );
lenstr = strlen(str + );
for (int ch = ; ch < ; ++ch) {
char cmp = ch + 'a';
for (int i = ; i <= lenstr; ++i) {
for (int k = ; k <= lenstr; ++k) {
if (cmp == str[i]) {
dp[i][k][ch] = dp[i - ][k][ch] + ;
} else if (k) dp[i][k][ch] = dp[i - ][k - ][ch] + ;
}
}
for (int k = ; k <= lenstr; ++k) {
int mx = ;
for (int i = ; i <= lenstr; ++i) {
mx = max(mx, dp[i][k][ch]);
}
ans[k][ch] = mx;
}
}
// cout << dp[2][0]['o' - 'a'] << endl;
// cout << dp[3][1]['o' - 'a'] << endl;
// cout << dp[4][1]['o' - 'a'] << endl;
int q;
scanf("%d", &q);
while (q--) {
char s[];
int m;
scanf("%d%s", &m, s);
printf("%d\n", ans[m][s[] - 'a']);
} } int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

C. An impassioned circulation of affection DP的更多相关文章

  1. 【Codeforces Round 418】An impassioned circulation of affection DP

                                                            C. An impassioned circulation of affection   ...

  2. codeforces 814 C. An impassioned circulation of affection 【尺取法 or DP】

    //yy:因为这题多组数据,DP预处理存储状态比每次尺取快多了,但是我更喜欢这个尺取的思想. 题目链接:codeforces 814 C. An impassioned circulation of ...

  3. An impassioned circulation of affection

    An impassioned circulation of affection time limit per test 2 seconds memory limit per test 256 mega ...

  4. Codeforces Round #418 (Div. 2) C. An impassioned circulation of affection

    C. An impassioned circulation of affection time limit per test 2 seconds memory limit per test 256 m ...

  5. 【尺取或dp】codeforces C. An impassioned circulation of affection

    http://codeforces.com/contest/814/problem/C [题意] 给定一个长度为n的字符串s,一共有q个查询,每个查询给出一个数字m和一个字符ch,你的操作是可以改变字 ...

  6. codeforces 814 C. An impassioned circulation of affection(二分+思维)

    题目链接:http://codeforces.com/contest/814/problem/C 题意:给出一串字符串然后q个询问,问替换掉将m个字符替换为字符c,能得到的最长的连续的字符c是多长 题 ...

  7. An impassioned circulation of affection(尺取+预处理)

    题目链接:http://codeforces.com/contest/814/problem/C 题目: 题意:给你一个长度为n的字符串,m次查询,每次查询:最多进行k步修改,求字符c(要输入的字符) ...

  8. CF814C An impassioned circulation of affection

    思路: 对于题目中的一个查询(m, c),枚举子区间[l, r](0 <= l <= r < n),若该区间满足其中的非c字符个数x不超过m,则可以将其合法转换为一个长度为r-l+1 ...

  9. Codeforces 814C - An impassioned circulation of affection

    原题链接:http://codeforces.com/contest/814/problem/C 题意:有长度为n的一个字符串,q个询问,每个询问由数字m和字符c组成,问最多在字符串中替换m个字符,使 ...

随机推荐

  1. codeforces 659A A. Round House(水题)

    题目链接: A. Round House time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. zk 04之 Zookeeper Api(java)与应用

    如何使用 Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储,但是 Zookeeper 并不是用来专门存储 ...

  3. Apache日志解读

    想要知道什么人在什么时候浏览了网站的哪些内容吗?查看Apache的访问日志就可以知道.访问日志是Apache的标准日志,本文详细解释了访问日志的内容以及相关选项的配置. 一.访问日志的格式  Apac ...

  4. Linux命令总结_touch创建文件

    1.touch命令,用来创建文件或者修改文件时间戳 格式:touch [选项]... 文件... 选项 : -a   或--time=atime或--time=access或--time=use  只 ...

  5. Linux命令总结_sort排序命令

    1.sort命令是帮我们依据不同的数据类型进行排序,其语法及常用参数格式: sort [-bcfMnrtk][源文件][-o 输出文件]       补充说明:sort可针对文本文件的内容,以行为单位 ...

  6. 02_mysql卸载和安装

    如果只是随便地反安装/uninstall之后,在文件系统或者是注册表里面可能会残留一些东西,这些东西如果不及时清除掉,再装可能会出现问题,你新装的会用不了. #Path to installation ...

  7. 《Java多线程编程核心技术》读后感(七)

    volatile关键字 主要作用是使变量在多个线程间可见. 关键字volatile与死循环 package Second; public class PrintString { private boo ...

  8. linux下安装mysql的三种方法:rpm包安装、yum安装、源码包安装

    1 安装MySQL数据库服务器安装方法一://查询系统自带的数据库rpm -qa | grep -i mysql //卸载查询到的所有mysqlrpm -e --nodeps mysql-libs-5 ...

  9. 7.20实习培训日志-Java基础程序设计结构

    Java基础程序设计结构 在 Math 类中,为了达到最快的性能,所有的方法都使用计算机浮点单元中的例程,如果得到一个完全可预测的结果比运行速度更重要的话,那么就应该使用StrictMath类,它使用 ...

  10. Invalidate()这个函数有什么用?

    c++中的这个函数,一会是刷新窗口的作用,一会是使区域无效.我搞不懂这个函数究竟是有什么作用?谢谢赐教. void Invalidate( BOOL bErase = TRUE ); 该函数的作用是使 ...