码迷,mamicode.com
首页 > 其他好文 > 详细

Zuhair and Strings-祖海和字符串 CodeForce#1105B

时间:2019-01-28 01:25:02      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:+=   and   hat   main   get   span   bst   ted   i++   

题目链接:Zuhair and Strings

题目原文

Given a string ?? of length ?? and integer ?? (1≤??≤??). The string ?? has a level ??, if ?? is largest non-negative integer, such that it‘s possible to find in ??:

?? non-intersecting (non-overlapping) substrings of length ??,
all characters of these ?? substrings are the same (i.e. each substring contains only one distinct character and this character is the same for all the substrings).
A substring is a sequence of consecutive (adjacent) characters, it is defined by two integers ?? and ?? (1≤??≤??≤??), denoted as ??[??…??] = "????????+1…????".

For example, if ??=2, then:

the string "aabb" has level 1 (you can select substring "aa"),
the strings "zzzz" and "zzbzz" has level 2 (you can select two non-intersecting substrings "zz" in each of them),
the strings "abed" and "aca" have level 0 (you can‘t find at least one substring of the length ??=2 containing the only distinct character).
Zuhair gave you the integer ?? and the string ?? of length ??. You need to find ??, the level of the string ??.

题目大意

找出字符串s里最多的连续k次出现的字母的出现次数,重叠部分的不算。

比如说aaabbaa,k=2。

找a的话:aaabbaa或者aaabbaa能找到两个。

找b的话:aaabbaa只有一个。

所以取最大值,2。

思路

用一个二维数组flag[x][y],x表示字母(x = (int)char - 97 )。y表示出现连续字母的个数的最大值。

比如aaabbaa中,flag[0][] = {0, 0, 3, 0, 0, 0, 2}, flag[1][] = {0, 0, 0, 0, 2, 0, 0}。

最终取Σ(flag[x][y]%2)最大值即可。

题解

 1 #include <iostream>
 2 #include <cstring>
 3 
 4 using namespace std;
 5 
 6 int len, k, ans, mmax;
 7 char c1, c2;
 8 
 9 int flag[30][200010];
10 
11 int main(int argc, char const *argv[])
12 {
13 #ifdef debug
14     freopen("test.txt", "r", stdin);
15 #endif
16     cin >> len >> k;
17     for(int i = 0; i < len; i++)
18     {
19         cin >> c2;
20         int index = c2 - 97;
21         if(c2 == c1)
22         {
23             flag[index][i] = flag[index][i-1]+1;
24             flag[index][i-1] = 0;
25         }
26         else
27         {
28             flag[index][i] = 1;
29         }
30         c1 = c2;
31     }
32     for (int i = 0; i < 30; ++i)
33     {
34         long long tmp = 0;
35         for (int j = 0; j < len; ++j)
36         {
37             tmp += flag[i][j] / k;
38         }
39         if(tmp > mmax)
40         {
41             mmax = tmp;
42         }
43     }
44     cout << mmax;
45     return 0;
46 }

 

Zuhair and Strings-祖海和字符串 CodeForce#1105B

标签:+=   and   hat   main   get   span   bst   ted   i++   

原文地址:https://www.cnblogs.com/SaltyFishQF/p/10327748.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!