Zuhair and Strings-祖海和字符串 CodeForce#1105B
Posted saltyfishqf
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }
以上是关于Zuhair and Strings-祖海和字符串 CodeForce#1105B的主要内容,如果未能解决你的问题,请参考以下文章
Swift学习笔记-教程学习二字符串和字符(Strings and Characters)
以撸代码的形式学习Swift-3:字符串和字符(Strings and Characters)
[CF1083B]The Fair Nut and Strings
codeforces 682D(Alyona and Strings)
LeetCode 205. Isomorphic Strings; 290. Word Pattern; 890. Find and Replace Pattern