Codeforces Round #621 (Div. 1 + Div. 2) C. Cow and Message
Bessie the cow has just intercepted a text that Farmer John sent to Burger Queen! However, Bessie is sure that there is a secret message hidden inside.
The text is a string ss of lowercase Latin letters. She considers a string tt as hidden in string ss if tt exists as a subsequence of ss whose indices form an arithmetic progression. For example, the string aab is hidden in string aaabb because it occurs at indices 11 , 33 , and 55 , which form an arithmetic progression with a common difference of 22 . Bessie thinks that any hidden string that occurs the most times is the secret message. Two occurrences of a subsequence of SS are distinct if the sets of indices are different. Help her find the number of occurrences of the secret message!
For example, in the string aaabb, a is hidden 33 times, b is hidden 22 times, ab is hidden 66 times, aa is hidden 33 times, bb is hidden 11 time, aab is hidden 22 times, aaa is hidden 11 time, abb is hidden 11 time, aaab is hidden 11 time, aabb is hidden 11 time, and aaabb is hidden 11 time. The number of occurrences of the secret message is 66 .
Input
The first line contains a string ss of lowercase Latin letters (1≤|s|≤1051≤|s|≤105 ) — the text that Bessie intercepted.
Output
Output a single integer — the number of occurrences of the secret message.
Examples
aaabb
6
usaco
1
lol
2
Note
In the first example, these are all the hidden strings and their indice sets:
- a occurs at (1)(1) , (2)(2) , (3)(3)
- b occurs at (4)(4) , (5)(5)
- ab occurs at (1,4)(1,4) , (1,5)(1,5) , (2,4)(2,4) , (2,5)(2,5) , (3,4)(3,4) , (3,5)(3,5)
- aa occurs at (1,2)(1,2) , (1,3)(1,3) , (2,3)(2,3)
- bb occurs at (4,5)(4,5)
- aab occurs at (1,3,5)(1,3,5) , (2,3,4)(2,3,4)
- aaa occurs at (1,2,3)(1,2,3)
- abb occurs at (3,4,5)(3,4,5)
- aaab occurs at (1,2,3,4)(1,2,3,4)
- aabb occurs at (2,3,4,5)(2,3,4,5)
- aaabb occurs at (1,2,3,4,5)(1,2,3,4,5)
Note that all the sets of indices are arithmetic progressions.
In the second example, no hidden string occurs more than once.
In the third example, the hidden string is the letter l.
大意是给定一个字符串,让你找出所有下标成等差数列的子串里出现次数最多的出现了多少次。
看了别人的博客才勉强搞懂-_- 首先要说的就是一个字母也算是n=1的等差数列。然后我们可以发现,n=1,2,3...当n>=3以后,形成等差数列的条件越强,一个n=3的等差数列可以拆分成三个n等于2的等差数列,因此我们可以直接忽略掉n>=3时的情况。n=1时,直接开一个数组single[26]统计每个字母出现的次数。n=2时会麻烦一点,这里使用一个二维数组cnt[26][26]统计,其含义为:cnt[i][j]代表字母对('a'+i, 'a'+j)出现的次数,一定注意顺序!然后扫一遍字符串,第一维获取当前字母,然后再写一个0~26的二重循环,更新a~z与当前字母组成的字母对的个数,注意当前字母是第二个。更新的核心操作是cnt[j][s[i]-'a']+=single[j] (i是第一维循环变量,j是第二维循环变量),直接加上j对应的字母之前出现的次数即可。
别忘开long long!!
#include <bits/stdc++.h>
using namespace std;
string s;
long long cnt[][]={};
int single[]={};
long long mmax(long long a, long long b)
{
return a>b?a:b;
}
int main()
{
cin>>s;
int i,j;
if(s.size()==)
{
cout<<;
return ;
}
long long ans=;
for(i=;i<s.size();i++)
{
for(j=;j<;j++)
{
cnt[j][s[i]-'a']+=single[j];
ans=mmax(ans,cnt[j][s[i]-'a']);
}
single[s[i]-'a']++;//更新n=1情况时一定要放在后面,要不然的话会被第二重循环统计到当前的字母
ans=mmax(ans,single[s[i]-'a']);
}
cout<<ans;
return ;
}
Codeforces Round #621 (Div. 1 + Div. 2) C. Cow and Message的更多相关文章
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- Educational Codeforces Round 39 (Rated for Div. 2) G
Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...
- Educational Codeforces Round 48 (Rated for Div. 2) CD题解
Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...
- Educational Codeforces Round 60 (Rated for Div. 2) 题解
Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...
随机推荐
- java_基础_注解
注解(annotation),不是注释(comment) 注解可以对程序做说明,这一点和注释一样但是,注解还可以被其他程序读取,这是注释所不具备的 内置注解:@Override(表示重写父类方法)—— ...
- Educational Codeforces Round 82 (Rated for Div. 2)E(DP,序列自动机)
#define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ],t[]; int n,m; ][]; ...
- 《E=MC2或一个思想的故事》
思想是起点.一切行动都以萌芽状态孕藏在思想之中,以往所做过的一切均离不开思想. 他是个纯朴的人,喜欢在乡下静静地冥想. .而科学家们却非常清楚,那些最伟大的成就都是在静默中完成的.
- BZOJ 2342 [Shoi2011]双倍回文(Manacher)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2342 题意:求最长子串使得它有四个相同的回文串SSSS相连组成. 首先跑一边Manach ...
- openfeign 使用方法和执行流程
1.用法 1.1引入依赖 <!-- feign client --> <dependency> <groupId>org.springframework.cloud ...
- 概率dp light 1321
题意:给定一张无向图,每条边都有一个通过的概率 ,如果无法通过,那么就要回到起点重新出发从起点到终点的时间固定为K,如果成功到达,又需要额外花费K的时间,问走S次的最小期望时间 思路:这道题分为两部分 ...
- vuex中怎么直接获取state中的值,以及computed的使用注意
1,直接用$store对象获取store对象,再进一步获取state属性..... 2, 3,computed computed是计算属性,他不可以直把值直接存入data中,因此不能像data一样直接 ...
- Nexus坑人系列-interface Unknown state L3 Not Ready
这个情况也容易出现在新使用设备的时候,当设备上没有L3接口模块的时候,这个问题就出现了. 接下来,尤其是如果我们需要运行VPC(并且如果正在运行N5K,N7K等!),则需要在交换机上配置第3层接口. ...
- 【PAT甲级】1110 Complete Binary Tree (25分)
题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点 ...
- 列表与数组 Perl入门第三章
列表List 是标量的有序集合.数组array则是存储列表的变量.数组/列表的每个元素element都是单独的标量变量,拥有独立的标量值. 1. 数组: 访问数组中的元素: $fred[0]=&quo ...