【思维构造】Cow and Message—CF1307C

Cow and Message---CF1307C

思路

看清题意!看清题意!看清题意!(重要事情说三遍)

一开始没看清题,以为要求的是所有下标呈等差数列的子串的总数,一直不知道怎么做。

后来看了题解才发现要求的是"所有下标呈等差数列的子串中哪个子串出现的次数最多"。

为了方便,下边称"下标呈等差数列的子串"为"满足条件的子串"。

弄明白了题意,我们现在只需要知道点就能做出来这道题了:满足条件的子串的长度一定是 1 1 1 或 2 2 2。

证明:

假设 s t r str str 是一个满足条件的子串,其中 s t r = a b c d e f g str~=~abcdefg str = abcdefg,那么 s t r str str 出现的次数一定 ≤ \le ≤ " a b " "ab" "ab" 出现的次数。

也就是说,任何一个满长度大于 2 2 2 的满足条件的子串出现的次数一定 ≤ \le ≤ 这个子串的任意一个长度为 2 2 2 的子序列出现的次数,这一点很好理解。原猜想得证。

C o d e Code Code

cpp 复制代码
#include <bits/stdc++.h>
#define int long long
#define sz(a) ((int)a.size())
#define all(a) a.begin(), a.end()
using namespace std;
using PII = pair<int, int>;
using i128 = __int128;
const int N = 2e5 + 10;

int n;

void solve(int Case) {
	string s; cin >> s;
	n = sz(s);
	s = " " + s;
	
	int res = 0;
	vector <int> num(27, 0);
	int ans[27][27] = {0};
	for (int i = 1; i <= n; i ++) {
		int ed = s[i] - 'a' + 1;
		for (int bg = 1; bg <= 26; bg ++) {
			ans[bg][ed] += num[bg];
			res = max(res, ans[bg][ed]);
		}
		num[s[i] - 'a' + 1] ++;
		res = max(res, num[s[i] - 'a' + 1]);
	}
	
	cout << "        ";
	cout << res << "\n";
}

signed main() {
	cin.tie(0)->ios::sync_with_stdio(false);
	int T = 1;
//	cin >> T; cin.get();
	int Case = 0;
	while (++ Case <= T) solve(Case);
	return 0;
}
相关推荐
无限进步_1 分钟前
【C++】巧用静态变量与构造函数:一种非常规的求和实现
开发语言·c++·git·算法·leetcode·github·visual studio
小超超爱学习993718 分钟前
大数乘法,超级简单模板
开发语言·c++·算法
xyx-3v2 小时前
qt创建新工程
开发语言·c++·qt
样例过了就是过了2 小时前
LeetCode热题100 爬楼梯
c++·算法·leetcode·动态规划
少司府2 小时前
C++基础入门:类和对象(中)
c语言·开发语言·c++·类和对象·运算符重载·默认成员函数
王老师青少年编程2 小时前
csp信奥赛c++之状压枚举
数据结构·c++·算法·csp·信奥赛·csp-s·状压枚举
ysa0510303 小时前
斐波那契上斐波那契【矩阵快速幂】
数据结构·c++·笔记·算法
6Hzlia5 小时前
【Hot 100 刷题计划】 LeetCode 45. 跳跃游戏 II | C++ 贪心算法最优解题解
c++·leetcode·游戏
森G5 小时前
48、柱状图---------QChart
c++·qt
Tanecious.5 小时前
蓝桥杯备赛:Day8-小苯的异或和
c++·蓝桥杯