CF814C An impassioned circulation of affection
思路:
对于题目中的一个查询(m, c),枚举子区间[l, r](0 <= l <= r < n),若该区间满足其中的非c字符个数x不超过m,则可以将其合法转换为一个长度为r-l+1的全c子序列,可以使用动态规划以O(n2)的复杂度计算,然而O(n2q)的复杂度还是太高了。于是先离线把26n中情况都计算出来,再打表即可。复杂度O(26n2+q)。
实现:
- #include <iostream>
- #include <cstdio>
- using namespace std;
- const int MAXN = ;
- int n, q, m, ans[][MAXN];
- string s;
- char c;
- int main()
- {
- cin >> n >> s;
- for (int k = ; k < ; k++)
- {
- char tmp = 'a' + k;
- for (int i = ; i < n; i++)
- {
- int cnt = ;
- for (int j = i; j < n; j++)
- {
- if (s[j] != tmp) cnt++;
- ans[k][cnt] = max(ans[k][cnt], j - i + );
- }
- }
- for (int i = ; i < MAXN; i++)
- ans[k][i] = max(ans[k][i], ans[k][i - ]);
- }
- cin >> q;
- for (int i = ; i < q; i++)
- {
- scanf("%d %c", &m, &c);
- printf("%d\n", ans[c - 'a'][m]);
- }
- }
CF814C An impassioned circulation of affection的更多相关文章
- An impassioned circulation of affection
An impassioned circulation of affection time limit per test 2 seconds memory limit per test 256 mega ...
- 【Codeforces Round 418】An impassioned circulation of affection DP
C. An impassioned circulation of affection ...
- codeforces 814 C. An impassioned circulation of affection 【尺取法 or DP】
//yy:因为这题多组数据,DP预处理存储状态比每次尺取快多了,但是我更喜欢这个尺取的思想. 题目链接:codeforces 814 C. An impassioned circulation of ...
- 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 ...
- An impassioned circulation of affection(尺取+预处理)
题目链接:http://codeforces.com/contest/814/problem/C 题目: 题意:给你一个长度为n的字符串,m次查询,每次查询:最多进行k步修改,求字符c(要输入的字符) ...
- C. An impassioned circulation of affection DP
http://codeforces.com/contest/814/problem/C 12ooyomioomioo21 o2 o 这题我是用dp解的,不过好像很慢,比赛的时候算了下不会mle,就没滚 ...
- 【尺取或dp】codeforces C. An impassioned circulation of affection
http://codeforces.com/contest/814/problem/C [题意] 给定一个长度为n的字符串s,一共有q个查询,每个查询给出一个数字m和一个字符ch,你的操作是可以改变字 ...
- codeforces 814 C. An impassioned circulation of affection(二分+思维)
题目链接:http://codeforces.com/contest/814/problem/C 题意:给出一串字符串然后q个询问,问替换掉将m个字符替换为字符c,能得到的最长的连续的字符c是多长 题 ...
- Codeforces 814C - An impassioned circulation of affection
原题链接:http://codeforces.com/contest/814/problem/C 题意:有长度为n的一个字符串,q个询问,每个询问由数字m和字符c组成,问最多在字符串中替换m个字符,使 ...
随机推荐
- MySQL使用教程收集(语法教程/命令教程)
说明:现在市面上的教程除了基本语法外,都基本是五花八门的,最权威且最全面的解释应该上官网去查看. https://www.tutorialspoint.com/mysql/index.htm http ...
- Servlet的调试
以下内容引用自http://wiki.jikexueyuan.com/project/servlet/debugging.html: 测试/调试Servlet始终是困难的.Servlets往往涉及大量 ...
- hibernate之多对一单向关联
一个工作组(Group)里能够有多个用户(User),一个User仅仅属于一个Group,这是典型的多对一的关系. 在多对一的关系中正确的数据库设计是在多的这方(在这里是User这方)加一个Group ...
- HDU 5319 Painter(枚举)
Painter Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Su ...
- struts1与struts2的差别
Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架.其全新的Struts 2的体系结构与Struts 1的体系结 ...
- Jquery第四课 Javascript中this的使用方法
thiskeyword的使用 在JavaScript中使用thiskeyword表示调用方法的对象,这与大部分面向对象语言是一样的.可是因为call.apply.bind等函数的影响.我们能够改变th ...
- jQuery無刷新上傳之uploadify簡單試用
先簡單的侃兩句:貌似已經有兩個月的時間沒有寫過文章了,不過仍會像以前那样每天至少有一至兩個小時是泡在园子裏看各位大神的文章.前些天在研究“ajax無刷新上傳”方面的一些插件,用SWFUpload實現了 ...
- Vijos 1451 圆环取数 【区间DP】
背景 小K攒足了路费来到了教主所在的宫殿门前,但是当小K要进去的时候,却发现了要与教主守护者进行一个特殊的游戏,只有取到了最大值才能进去Orz教主…… 描述 守护者拿出被划分为n个格子的一个圆环,每个 ...
- CANopen——笔记
1. c语言的typedef高级用法 typedef void (*post_sync_t)(CO_Data*); http://zhidao.baidu.com/link?url=_lDBGq_uk ...
- POJ 1477:Box of Bricks
Box of Bricks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19949 Accepted: 8029 De ...