【思维构造】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++ STL常用容器总结(vector, deque, list, map, set)
c++·算法
liulilittle2 小时前
C++ TAP(基于任务的异步编程模式)
服务器·开发语言·网络·c++·分布式·任务·tap
励志要当大牛的小白菜4 小时前
ART配对软件使用
开发语言·c++·qt·算法
PAK向日葵5 小时前
【算法导论】如何攻克一道Hard难度的LeetCode题?以「寻找两个正序数组的中位数」为例
c++·算法·面试
QQ_4376643149 小时前
C++11 右值引用 Lambda 表达式
java·开发语言·c++
liulilittle11 小时前
C++/CLI与标准C++的语法差异(一)
开发语言·c++·.net·cli·clr·托管·原生
小狄同学呀11 小时前
VS插件报错,g++却完美编译?API调用错因分析
c++
程序员编程指南11 小时前
Qt 数据库连接池实现与管理
c语言·数据库·c++·qt·oracle
小乖兽技术11 小时前
C#与C++交互开发系列(二十四):WinForms 应用中嵌入C++ 原生窗体
c++·c#·交互
张北北.11 小时前
【深入底层】C++开发简历4+4技能描述6
java·开发语言·c++