Zuhair and Strings-祖海和字符串 CodeForce#1105B
题目链接: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 }