题目链接:http://codeforces.com/contest/814/problem/C
题目:
题意:给你一个长度为n的字符串,m次查询,每次查询:最多进行k步修改,求字符c(要输入的字符)最长连续的长度。
思路:尺取法,预处理出每个字符连续长度为k需要的代价,然后O1查询。
代码实现如下:
1 #include <set> 2 #include <map> 3 #include <queue> 4 #include <stack> 5 #include <cmath> 6 #include <bitset> 7 #include <cstdio> 8 #include <string> 9 #include <vector> 10 #include <cstdlib> 11 #include <cstring> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 16 typedef long long ll; 17 typedef pair<ll, ll> pll; 18 typedef pair<ll, int> pli; 19 typedef pair<int, ll> pil;; 20 typedef pair<int, int> pii; 21 typedef unsigned long long ull; 22 23 #define lson i<<1 24 #define rson i<<1|1 25 #define bug printf("********* "); 26 #define FIN freopen("D://code//in.txt", "r", stdin); 27 #define debug(x) cout<<"["<<x<<"]" <<endl; 28 #define IO ios::sync_with_stdio(false),cin.tie(0); 29 30 const double eps = 1e-8; 31 const int mod = 1000000007; 32 const int maxn = 1500 + 7; 33 const double pi = acos(-1); 34 const int inf = 0x3f3f3f3f; 35 const ll INF = 0x3f3f3f3f3f3f3f; 36 37 int n, q, x, cnt; 38 char s[maxn], c[5]; 39 int ans[26][maxn]; 40 41 int main() { 42 //FIN; 43 scanf("%d%s", &n, s); 44 for(int i = 0; i < 26; i++) { 45 for(int j = 0; j < n; j++) { 46 cnt = 0; 47 for(int k = j; k < n; k++) { 48 if(s[k] != 'a' + i) cnt++; 49 ans[i][cnt] = max(ans[i][cnt], k - j + 1); 50 } 51 } 52 for(int j = 1; j <= n; j++) { 53 ans[i][j] = max(ans[i][j], ans[i][j-1]); 54 } 55 } 56 scanf("%d", &q); 57 while(q--) { 58 scanf("%d%s", &x, c); 59 int num = c[0] - 'a'; 60 printf("%d ", ans[num][x]); 61 } 62 return 0; 63 }