栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Codeforces Round #747 (Div. 2), problem: (C) Make Them Equal,

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Codeforces Round #747 (Div. 2), problem: (C) Make Them Equal,

C. Make Them Equal
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Theofanis has a string s1s2…sn and a character c. He wants to make all characters of the string equal to c using the minimum number of operations.

In one operation he can choose a number x (1≤x≤n) and for every position i, where i is not divisible by x, replace si with c.

Find the minimum number of operations required to make all the characters equal to c and the x-s that he should use in his operations.

Input
The first line contains a single integer t (1≤t≤104) — the number of test cases.

The first line of each test case contains the integer n (3≤n≤3⋅105) and a lowercase Latin letter c — the length of the string s and the character the resulting string should consist of.

The second line of each test case contains a string s of lowercase Latin letters — the initial string.

It is guaranteed that the sum of n over all test cases does not exceed 3⋅105.

Output
For each test case, firstly print one integer m — the minimum number of operations required to make all the characters equal to c.

Next, print m integers x1,x2,…,xm (1≤xj≤n) — the x-s that should be used in the order they are given.

It can be proved that under given constraints, an answer always exists. If there are multiple answers, print any.

Example
inputCopy
3
4 a
aaaa
4 a
baaa
4 b
bzyx
outputCopy
0
1
2
2
2 3
Note
Let’s describe what happens in the third test case:

x1=2: we choose all positions that are not divisible by 2 and replace them, i. e. bzyx → bzbx;
x2=3: we choose all positions that are not divisible by 3 and replace them, i. e. bzbx → bbbb.
题意:给定一个字符串和一个给定的字符和任意给定的m个的x,需要将给定的字符串变成有给定字符的串,对于在字符串中和给定字符不一样的字符的替换规则是:当前字符的索引不能被给出的x整除就给定的字符进行替换,求此时能得到目标字符串的最小操作次数并将m个x进行相关的输出即可。
题解:我们通过发现题目的相关性质,此时的操作数无非就是三种情况:0、1、2,三种,根据整除的相关性质我们可以发现:我们可以通过判断某个数的倍数是否都是给定字符,如果符合前面的条件的话,那么这个数就是满足1个操作数的x值,否则的话就是两个操作数,对于两个操作,我们取n和n-1两个最大的数就能完成变化字符串;0的情况就是说明此时不需要进行字符串的变换,即原来的字符串是符合题意的给定字符的串的集合。
代码:

#include
using namespace std;
const int N = 3e5 + 5;
int n;
char s[N], x[3], c;
int main() {
	int t;
	cin>>t;
	while (t--) {
		cin>>n;
		// cin>>c>>s;
		bool flag = false;
		int p;
		scanf("%s%s", x, s + 1);
		c = x[0];
		bool cnt=false;
		for (int i = 1; i <= n; i++)
			if (s[i] != c) cnt=true;
		if (!cnt) cout<<0<
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/302915.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号