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.
The first line contains a string ss of lowercase Latin letters (1≤|s|≤1051≤|s|≤105) — the text that Bessie intercepted.
Output a single integer — the number of occurrences of the secret message.
aaabb
6
usaco
1
lol
2
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)
In the second example, no hidden string occurs more than once.
In the third example, the hidden string is the letter l.
不知道怎么证明的两或一个字符的时候最大,与顺序有关的一般用前缀和或后缀和统计
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> #include <queue> #include <map> #include <sstream> #include <cstdio> #include <cstring> #include <numeric> #include <cmath> #include <iomanip> #include <deque> #include <bitset> #define ll long long #define PII pair<int, int> #define rep(i,a,b) for(int i=a;i<=b;i++) #define dec(i,a,b) for(int i=a;i>=b;i--) #define pb push_back #define mk make_pair using namespace std; int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } }; const long long INF = 0x7f7f7f7f7f7f7f7f; const int inf = 0x3f3f3f3f; const double pi = 3.14159265358979323846; const int mod = 998244353; const int N = 2e5 + 5; //if(x<0 || x>=r || y<0 || y>=c) inline ll read() { ll x = 0; bool f = true; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return f ? x : -x; } ll gcd(ll m, ll n) { return n == 0 ? m : gcd(n, m % n); } ll lcm(ll m, ll n) { return m * n / gcd(m, n); } ll a2[26][26],a1[26]; int main() { string s; cin >> s; for (int i = 0; i < s.size(); i++) { int c = s[i] - 'a'; for (int j = 0; j < 26; j++) { a2[j][c] += a1[j]; } a1[c]++; } ll ans = 0; for (int i = 0; i < 26; i++) { ans = max(ans, a1[i]); } for (int i = 0; i < 26; i++) { for (int j = 0; j < 26; j++) { ans = max(ans, a2[i][j]); } } cout << ans << endl; return 0; }
原文地址:https://www.cnblogs.com/dealer/p/12963381.html
- python 利用random生成验证码与MD5码加密过程
- Java内部类的继承
- Java继承类中static成员函数的重写
- Search for a range寻找上下界-Leetcode
- Basic Calculator 基本计算器-Leetcode
- python yield函数深入浅出理解
- 十分钟搞定 Tensorflow 服务
- datapump跨平台升级迁移的总结 (r8笔记第77天)
- Java中isAssignableFrom()方法与instanceof()方法用法
- 与Ajax同样重要的jQuery(1)
- Java中Class类详解、用法及泛化
- python 函数编程的位置参数、默认参数、关键字参数以及函数的递归
- Java子类的父类和要实现的接口有相同的方法/函数会冲突吗
- Java关键字 Finally执行与break, continue, return等关键字的关系
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Android VideoCache视频缓存的方法详解
- Android标题栏最右边添加按钮的实例
- Ubuntu18.04系统安装、配置Redis及phpredis扩展操作详解
- Android电话拨号器实例详解
- Android获取App内存使用情况的方法
- linux后台运行的几种方式(小结)
- Android 实现ListView的点击变色的实例
- Android播放音乐案例分享
- linux crm部署代码详解
- Android自定义WaveProgressView实现水波纹加载需求
- CentOS8下的root密码快速修改方法
- Android开发之自定义刮刮卡实现代码
- Android ScrollView无法填充满屏幕的解决办法
- Android 监听屏幕是否锁屏的实例代码
- Android实现水波纹控件的方法