【思维构造】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

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值