1020 Delete At Most Two Characters(35 分)

本文介绍了一个算法问题:给定仅包含小写英文字母的字符串,删除最多两个字符后能得到多少种不同的字符串。通过前后扫描并记录相同字符的出现情况来高效解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1020 Delete At Most Two Characters(35 分)
Given a string which contains only lower case English letters, how many different strings you can get after deleting AT MOST TWO characters in it?

Input Specification:
Each input file contains one test case, which gives the string whose length is in [3,10
​6
​​ ].

Output Specification:
Print in a line the number of different strings you can get after deleting at most 2 characters.

Sample Input:
ababcc
Sample Output:
15
Hint:
Deleting 0 character gets ababcc.

Deleting 1 character gets babcc, aabcc, abbcc, abacc and ababc.

Deleting 2 character gets abcc, bbcc, bacc, babc, aacc, aabc, abbc, abac and abab.

倒着扫一遍后缀相同的值
正向再扫一遍就行了。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define N (int)2e6+10
char str[N];
int post[N];
int main(void)
{
	scanf("%s", str+1);
	int i, j;
	ll res = 1;
	int len = strlen(str+1);
	post[len] = 1;
	for (i = len; i >= 2; i--)
	{
		if (str[i] != str[i-1]) post[i-1] = post[i]+1;
		else post[i-1] = post[i];
	}
	for (i = 1; i <= len; i++)
	{
		if (str[i] == str[i-1]) continue;
		res += post[i+1];
		
		if (i >= 2 && str[i-1] == str[i+1] || str[i-1] == str[i] && str[i] == str[i+1]) res--;
	}
	res += post[1];
	cout << res;
	
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值