D.Say No to Palindromes(前缀和优化查询

该博客主要讨论如何处理一个字符串,使其不包含长度大于等于2的回文子串。通过模拟好串的特征,发现好串仅由'abc'循环构成,并使用动态规划维护修改次数的前缀和。在给定区间内查询最少修改次数,以实现字符串转化为好串。

D. Say No to Palindromes
题意:
定义一个字符串是好串,当且仅当 s s s 不包含任何长度大于等于 2 2 2 的回文子串。
给定一个长度为 n n n 的字符串(仅包含 a , b , c a,b,c a,b,c 三种字符)和 m m m 个询问,每次询问查询一个区间 [ l , r ] [l,r] [l,r],求将字符串 S l . . . r S_{l...r} Sl...r 变成好串至少需要修改多少字符。
思路:
因为字符串的组成仅有 3 3 3 种字符
模拟一下好串的特征,初始为 a a a,添加一个 b b b,下一个只能添加 c c c,否则 a b a aba aba 或者 a b b abb abb 都不是好串,再往后加只能加 a a a,就进入循环了,也就是好串仅有 a b c abc abc 这样的子串循环构成, a b c a b c a abcabca abcabca
a b c abc abc 这样的子串有 A 3 3 A_3^3 A33
维护一个由第 j j j 种子串构成字符串时,需要修改的字符个数的前缀和
code:

#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define ld long double
#define all(x) x.begin(), x.end()
#define mem(x, d) memset(x, d, sizeof(x))
#define eps 1e-6
using namespace std;
const int maxn = 2e5 + 9;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll n, m;
string s;
char t[6][5] = {"abc", "acb", "bac", "bca", "cab", "cba"};
int f[maxn][6];

void work()
{
	cin >> n >> m;
	cin >> s; s = "@" + s;
	for(int i = 1; i <= n; ++i){
		int k = (i - 1) % 3;
		for(int j = 0; j < 6; ++j)
			f[i][j] = f[i-1][j] + (s[i] != t[j][k]);
	}
	while(m--){
		int l, r;cin >> l >> r;
		int ans = inf;
		for(int i = 0; i < 6; ++i) ans = min(ans, f[r][i] - f[l-1][i]);
		cout << ans << endl;
	}	
}

int main()
{
	ios::sync_with_stdio(0);
//	int TT;cin>>TT;while(TT--)
	work();
	return 0;
}
### 三位回文数的定义 三位回文数是指一个三位数,其正读和反读都相同。例如 101、222、343 等,这些数从左向右读和从右向左读的结果是一样的。 ### 三位回文数的判断方法 判断一个三位数是否为回文数,可以通过比较其百位和个位数字是否相同。因为三位数的形式为 $abc$($a$ 为百位数字,$b$ 为十位数字,$c$ 为个位数字,且 $1\leq a\leq9$,$0\leq b\leq9$,$0\leq c\leq9$),若 $a = c$,则该三位数就是回文数。以下是用 Python 实现的代码: ```python def is_three_digit_palindrome(num): if 100 <= num <= 999: hundreds = num // 100 units = num % 10 return hundreds == units return False # 测试示例 print(is_three_digit_palindrome(121)) print(is_three_digit_palindrome(123)) ``` 也可以使用 C 语言实现,代码如下: ```c #include <stdio.h> #include <stdbool.h> bool is_three_digit_palindrome(int num) { if (num >= 100 && num <= 999) { int hundreds = num / 100; int units = num % 10; return hundreds == units; } return false; } int main() { printf("%d\n", is_three_digit_palindrome(121)); printf("%d\n", is_three_digit_palindrome(123)); return 0; } ``` ### 三位回文数的生成方法 三位回文数的百位可以从 1 到 9 取值,十位可以从 0 到 9 取值,个位与百位相同。因此,可以通过两层循环来生成所有的三位回文数。以下是 Python 代码示例: ```python three_digit_palindromes = [] for hundreds in range(1, 10): for tens in range(0, 10): num = hundreds * 100 + tens * 10 + hundreds three_digit_palindromes.append(num) print(three_digit_palindromes) ``` C 语言实现代码如下: ```c #include <stdio.h> int main() { for (int hundreds = 1; hundreds < 10; hundreds++) { for (int tens = 0; tens < 10; tens++) { int num = hundreds * 100 + tens * 10 + hundreds; printf("%d ", num); } } return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值