Codeforces Round #747 (Div. 2) 题解C - Make Them Equal

Problem - C - Codeforces

思路分析:

首先我们要求出最小次数,可以得知我们最多只需要两步,取x = n 和 x = n - 1 则一定能符合题意

因此我们只需判断是否能通过0次或1次来满足题意

1、对于0次

我们只需要先遍历一遍字符数组,看看是否有不满足的即可

2、对于1次

我们可以遍历所有的x(从1 - n),只要有一个x能满足所有x的倍数位置上的字符都与c相同,那么我们就一定能只用x使不符合题意位置上的字符变为c

3、如果上述两种情况都不满足,则输出2、n和n-1即可

代码示例:

//#pragma comment(linker,   "/STACK:10240000000000,10240000000000")
//#pragma GCC optimize(2)

#include <bits/stdc++.h>

#define For(i,a,b) for (int i=(a);i<=(b);++i)
#define Fod(i,b,a) for (int i=(b);i>=(a);--i)
#define mls multiset
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define pob pop_back
#define itt iterator
#define lowbit(x) x & (-x)
#define clr(x) memset(x, 0, sizeof(x));

typedef long long ll;
typedef unsigned long long ull;
		
using namespace std;
const int MAXN = 0x7fffffff;
const int MOD = 1000000007;
const int N = 3e5 + 5;
int main ()
{	
	ios::sync_with_stdio(false);
	cin.tie(0);

	int t;
	cin >> t;
	while(t--)
	{
		int n;
		char s[N];
		char c;
		cin >> n >> c;
		For(i, 1, n) cin >> s[i];
		bool ok = true;
		bool p = true;
		For(i, 1, n)
			if(s[i] != c)
			{
				ok = false;	
				break;
			}
		if(ok)
		{
			cout << 0 << endl;
			continue;
		}
		else 
		{
			For(i, 1, n)
			{
				bool flag = true;
				int ans = i;
				for(int j = i; j <= n; j += i)
				{
					if(s[j] != c)
					{
						flag = false;
						break;
					}
				}
				if(flag)
				{
					cout << 1 << endl;
					cout << ans << endl; 
					p = false;
					break;
				}
			}
		}
		if(p)
		{
			cout << 2 << endl;
			cout << n-1 << " " << n << endl;
		}
	}
	return 0;
}	
posted @ 2021-11-08 15:59  Yra  阅读(87)  评论(0)    收藏  举报