WC analog (1.2) T1 string

strand

Background:

1.2 WC analog T1

Analysis: conclusion analysis

 

It seems that the answer can only be - 1,1,2, and then it directly judges 1, then n2 enumerates, delete where to delete, and then 60 points

If s is not a palindrome string, then the answer is obviously 1, then for 1 < I < n, if there i s an I that satisfies s[1..i] and s[i +I n 1..n], neither of them i s a palindrome string, so the answer should be 2. If any I satisfies at least one of them is a palindrome string, after discussion, it can be found that if s[1] ==s[2], then s should be in the shape of aaaaaaaa (all the same), or aaabaaa (the length is odd, only the middle one is different), if s[1] ! = s[2], then s shape is like abababa (palindrome string with odd length and alternating two letters). All three cases will be unsolved, so we only need to judge 1 (not palindrome string), - 1 (above three cases) and other output 2······

Source:

 

/*
	created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>
#include <ctime>
#include <bitset>

inline char read() {
	static const int IN_LEN = 1024 * 1024;
	static char buf[IN_LEN], *s, *t;
	if (s == t) {
		t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
		if (s == t) return -1;
	}
	return *s++;
}

///*
template<class T>
inline void R(T &x) {
	static char c;
	static bool iosig;
	for (c = read(), iosig = false; !isdigit(c); c = read()) {
		if (c == -1) return ;
		if (c == '-') iosig = true;	
	}
	for (x = 0; isdigit(c); c = read()) 
		x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
//*/

const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {
	if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
	*oh++ = c;
}

template<class T>
inline void W(T x) {
	static int buf[30], cnt;
	if (x == 0) write_char('0');
	else {
		if (x < 0) write_char('-'), x = -x;
		for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
		while (cnt) write_char(buf[cnt--]);
	}
}

inline void flush() {
	fwrite(obuf, 1, oh - obuf, stdout);
}

/*
template<class T>
inline void R(T &x) {
	static char c;
	static bool iosig;
	for (c = getchar(), iosig = false; !isdigit(c); c = getchar())
		if (c == '-') iosig = true;	
	for (x = 0; isdigit(c); c = getchar()) 
		x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
//*/

const int MAXN = 100000 + 10;

int n, t;
char s[MAXN];

inline void solve() {
	scanf("%d%s", &n, s);
	bool flag = false ; 
	for (int i = 0; i < n; ++i) 
		if (s[i] != s[n - i - 1]) {
			flag = true; 
			break ;
		}
	if (flag) {
		W(1), write_char('\n');
		return ;
	}
	if (n & 1) {
		bool flag;
		flag = true ;
		for (int i = 0; i < n; ++i) 
			if (s[i] != s[i % 2]) {
				flag = false;
				break ;
			}
		if (flag) {
			W(-1), write_char('\n');
			return ;
		}
		flag = true ;
		for (int i = 0; i < n; ++i)
			if (i != n / 2 && s[i] != s[0]) {
				flag = false;
				break ;
			}
		if (flag) W(-1), write_char('\n');
		else W(2), write_char('\n');
	} else {
		bool flag = true ;
		for (int i = 0; i < n; ++i)
			if (s[i] != s[0]) {
				flag = false;
				break ;
			}
		if (flag) W(-1), write_char('\n');
		else W(2), write_char('\n');
	}
}

int main() {
	freopen("string.in", "r", stdin);
	freopen("string.out", "w", stdout);
	scanf("%d", &t);
	while (t--) solve();
	flush();
	return 0;
}

Posted by Stryker250 on Fri, 01 May 2020 02:50:45 -0700