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

  1. #include <bits/stdc++.h>
  2. #define IOS ios::sync_with_stdio(false)
  3. using namespace std;
  4. #define inf (0x3f3f3f3f)
  5. typedef long long int LL;
  6. char str[];
  7. int lenstr;
  8. //void dfs(char ch, int cur, int nowLen, int did, bool can) {
  9. // if (did > lenstr) return;
  10. // dp[ch][did] = max(dp[ch][did], nowLen);
  11. // if (dp[ch][did] > nowLen && nowLen && can) return;
  12. // if (cur > lenstr) return;
  13. // if (str[cur] == ch) {
  14. // dfs(ch, cur + 1, nowLen + 1, did, false);
  15. // } else {
  16. // dfs(ch, cur + 1, 0, did, false);
  17. // dfs(ch, cur + 1, nowLen + 1, did + 1, true);
  18. //
  19. // }
  20. //}
  21. int dp[ + ][ + ][ + ];
  22. int ans[ + ][ + ];
  23. void work() {
  24. int n;
  25. scanf("%d", &n);
  26. scanf("%s", str + );
  27. lenstr = strlen(str + );
  28. for (int ch = ; ch < ; ++ch) {
  29. char cmp = ch + 'a';
  30. for (int i = ; i <= lenstr; ++i) {
  31. for (int k = ; k <= lenstr; ++k) {
  32. if (cmp == str[i]) {
  33. dp[i][k][ch] = dp[i - ][k][ch] + ;
  34. } else if (k) dp[i][k][ch] = dp[i - ][k - ][ch] + ;
  35. }
  36. }
  37. for (int k = ; k <= lenstr; ++k) {
  38. int mx = ;
  39. for (int i = ; i <= lenstr; ++i) {
  40. mx = max(mx, dp[i][k][ch]);
  41. }
  42. ans[k][ch] = mx;
  43. }
  44. }
  45. // cout << dp[2][0]['o' - 'a'] << endl;
  46. // cout << dp[3][1]['o' - 'a'] << endl;
  47. // cout << dp[4][1]['o' - 'a'] << endl;
  48. int q;
  49. scanf("%d", &q);
  50. while (q--) {
  51. char s[];
  52. int m;
  53. scanf("%d%s", &m, s);
  54. printf("%d\n", ans[m][s[] - 'a']);
  55. }
  56.  
  57. }
  58.  
  59. int main() {
  60. #ifdef local
  61. freopen("data.txt", "r", stdin);
  62. // freopen("data.txt", "w", stdout);
  63. #endif
  64. work();
  65. return ;
  66. }

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. Eclipse_插件_02_jd-eclipse插件的安装

    1.去官网下载 jd-eclipse插件 2.解压后的文件夹A放到eclipse的drops文件夹下 3.删掉多余文件,确保文件夹A下只有plugin 和 freature 两个文件夹 4.清空osg ...

  2. SSH中的Hibernate

    SSH中的Hibernate 就是DAO连接数据库对数据进行实际操作,做了架构简化,对数据库的操作.

  3. PHP 正则表达示

    PHP 正则表达示 php如何使用正则表达式 正则表达式基本元字符 #正则表达式示例 ^:匹配输入字符串开始的位置.如果设置了 RegExp 对象的 Multiline 属性,^ 还会与“\n”或“\ ...

  4. BJOI2018爆零记

    没啥可说的 Day1 0分 T1 给你一个二进制串,每次修改一个位置,询问[l,r]区间中有多少二进制子串重排后能被3整除 T2 一个无向图(无重边自环)每个点有一个包含两种颜色的染色集合,一个边的两 ...

  5. 记一次keepalived脑裂问题查找

    在自己环境做keepalived+Redis实验时,当重启了备用redies机器后,发现两台redies主机都拿到了VIP [root@redis2 ~]# ip addr list 1: lo: & ...

  6. VijosP1250:分组背包

    背景 Wind设计了很多机器人.但是它们都认为自己是最强的,于是,一场比赛开始了~ 描述 机器人们都想知道谁是最勇敢的,于是它们比赛搬运一些物品. 它们到了一个仓库,里面有n个物品,每个物品都有一个价 ...

  7. 转:isualvm远程监控Tomcat

    一.Java VisualVM 概述 对于使用命令行远程监控jvm 太麻烦 . 在jdk1.6 中 Oracle 提供了一个新的可视化的. JVM 监控工具 Java VisualVM .jvisua ...

  8. Oracle系统权限列表

    当你新建一个用户,指定表空间之后,这个用户基本上什么都不能做,连接数据库都不可以.你要给这个用户赋各种权限. create session     -----允许用户连接到数据 create tabl ...

  9. linux安装AWStats业务数据分析工具

    Awstats是一个非常简洁而且强大的统计工具.它可以统计您站点的如下信息:一:访问量,访问次数,页面浏览量,点击数,数据流量等精确到每月.每日.每小时的数据二:访问者国家.访问者IP.操作系统.浏览 ...

  10. 点击实现CSS样式切换

    如图所示 代码如下图: 特别要注意的是:a标签不会继承上级的color,所以要单独为其设置 参看代码(并非上图代码)如下: <!DOCTYPE html> <html> < ...