16CF2--1003
来源:互联网 发布:传统企业转型网络案例 编辑:程序博客网 时间:2024/06/05 04:32
Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it!
Still unsatisfied with the garland, Nadeko decided to polish it again. The garland has n pieces numbered from 1 to n from left to right, and the i-th piece has a colour si, denoted by a lowercase English letter. Nadeko will repaint at most m of the pieces to give each of them an arbitrary new colour (still denoted by a lowercase English letter). After this work, she finds out all subsegments of the garland containing pieces of only colour c — Brother Koyomi's favourite one, and takes the length of the longest among them to be the Koyomity of the garland.
For instance, let's say the garland is represented by "kooomo", and Brother Koyomi's favourite colour is "o". Among all subsegments containing pieces of "o" only, "ooo" is the longest, with a length of 3. Thus the Koyomity of this garland equals 3.
But problem arises as Nadeko is unsure about Brother Koyomi's favourite colour, and has swaying ideas on the amount of work to do. She has q plans on this, each of which can be expressed as a pair of an integer mi and a lowercase letter ci, meanings of which are explained above. You are to find out the maximum Koyomityachievable after repainting the garland according to each plan.
The first line of input contains a positive integer n (1 ≤ n ≤ 1 500) — the length of the garland.
The second line contains n lowercase English letters s1s2... sn as a string — the initial colours of paper pieces on the garland.
The third line contains a positive integer q (1 ≤ q ≤ 200 000) — the number of plans Nadeko has.
The next q lines describe one plan each: the i-th among them contains an integer mi(1 ≤ mi ≤ n) — the maximum amount of pieces to repaint, followed by a space, then by a lowercase English letter ci — Koyomi's possible favourite colour.
Output q lines: for each work plan, output one line containing an integer — the largest Koyomity achievable after repainting the garland according to it.
6koyomi31 o4 o4 m
365
15yamatonadeshiko101 a2 a3 a4 a5 a1 b2 b3 b4 b5 b
3457812345
10aaaaaaaaaa210 b10 z
1010
In the first sample, there are three plans:
- In the first plan, at most 1 piece can be repainted. Repainting the "y" piece to become "o" results in "kooomi", whose Koyomity of 3 is the best achievable;
- In the second plan, at most 4 pieces can be repainted, and "oooooo" results in a Koyomity of 6;
- In the third plan, at most 4 pieces can be repainted, and "mmmmmi" and "kmmmmm" both result in a Koyomity of 5.
题解:http://codeforces.com/blog/entry/52449
题意:
给出字符串,然后q种plan,每种的m是可以修改次数,字符c是koyomi喜欢的字符,问修改后能达到的最长c子串有多长
解题思路:
最近在做搜索专题,看到什么题都想搜索,虽然这不是搜索题,这个题感觉自己的搜索方法应该对了,就是TLE在test7,
其实这个题是用两个指针l和r来模拟子串首尾,并用循环找最大值,
就是一把尺子(固定某一条件,修改字符数为m),不断向右移动,不断更新我们要的答案。在这里,我们只要从左往右,让修改的字符个数从0慢慢增加到m,中途将字符改成同一个字符c,最后修改字符数固定为m,每次向右移动时,如果字符串需要修改,那就改掉右面的字符,将之前最左边的字符换回来。
我们从左边开始,扫描字符串。如果遇到c,那就把c丢进队列,如果遇到!c,且此时队列中!c的个数不超过m的话,就把!c丢进去。如果!c的个数等于m,且又遇到一个!c,那就记录下此时队列长度,这个时候队列里m个!c可以当作c,所以可以记录队列元素个数,更新答案最大值。之后将队列前面元素弹出,直到弹出一个!c,再将新的!c压进队列……一直扫描直到字符串尾。
取尺法例子:http://blog.csdn.net/qq_34472846/article/details/51990694
代码(尺取法):
#include<iostream>#include<string>#include<cstdio>#include<algorithm>#include<cmath>#include<iomanip>#include<queue>#include<cstring>using namespace std;int i,mx,l,r,k,q,m,n;char c,s[1510];int main(){ scanf("%d",&n); scanf("%s",s+1); scanf("%d",&q); while(q--) { scanf("%d %c",&m,&c); mx=k=0; l=1; for(r=1;r<=n;r++) { if(s[r]!=c) k++; while(k>m) { if(s[l]!=c) k--; l++; } mx=max(mx,r-l+1); } printf("%d\n",mx); } return 0;}
代码(超时深搜):
#include<iostream>#include<string>#include<cstdio>#include<algorithm>#include<cmath>#include<iomanip>#include<queue>#include<cstring>using namespace std;int n,q,cnt,m;char c,s[1505],ss[1505];bool flag;int fnum(){ int sum=0,maxn=0; for(int i=1;i<=n;i++) { if(ss[i]==c&&ss[i-1]==c) {sum++; if(maxn<sum) maxn=sum;} else { if(ss[i]==c&&ss[i-1]!=c) {sum=1; if(maxn<1) maxn=1;} } } return maxn;}bool all(){ for(int i=1;i<=n;i++) if(ss[i]!=c) return false; return true;}void dfs(int i,int b){ if(flag) return; for(int j=b;j<=n;j++) { if(ss[j]!=c) { char temp=ss[j]; ss[j]=c; if(all()) {flag=true; cnt=n; return;} if(i==m) { int xx=fnum(); if(cnt<xx) { cnt=xx; } } else { dfs(i+1,j+1); if(flag) return; } ss[j]=temp; } }}int main(){ scanf("%d",&n); getchar(); s[0]='0'; for(int i=1;i<=n;i++) scanf("%c",&s[i]); scanf("%d",&q); for(int i=1;i<=q;i++) { for(int j=1;j<=n;j++) ss[j]=s[j]; scanf("%d",&m); cin>>c; if(all()) {printf("%d\n",n); continue;} cnt=0; flag=false; dfs(1,1); printf("%d\n",cnt); } return 0;}
- 16CF2--1003
- 16CF2--1001--(dfs?)
- 16CF2--1002--(dfs)
- CF2 A + B + C
- (CF2,包含尺度变化)tracker_ensemble程序简介
- Hierarchical Convolutional Features for Visual Tracking(CF2)论文翻译
- 在.NET CF2.0中调用DirectShow来处理视音频数据
- hduoj5088 Problem5088 BestCoder Round #16 1003
- 1003
- 1003
- 1003
- 1003
- 1003
- 1003
- 1003
- 1003
- 1003
- 1003
- 重写Hashcode()和equals()
- cocos2d打包apk,分辨率问题
- Spring boot redis的使用(二)
- poj 3844 Divisible Subsequences(数学+抽屉原理)
- 获取手机WIFI 数据的 IP
- 16CF2--1003
- PAT【1004】成绩排名【sort】
- CentOS7 安装Ngin
- Linux平台Cpu使用率的计算
- struts2重点、精华、用法总结(一)
- (6)Array对象
- C语言中volatile关键字的作用
- 关注民生民情——华北水利水电大学“情艺”国情社情调查
- Android Studio用小米手机调试安装不上应用error while installing apks