WC模拟(1.2) T1 串

题目背景:

1.2 WC模拟T1

分析:结论分析

 

考场上打表发现,貌似答案只能是-1,1,2,然后就直接判了一下1,然后n2枚举了一下,删除删哪里,然后60分…

正解考虑,如果s不是回文串,那么答案显然是1,那么考虑对于1 <= i < n,如果存在一个i满足s[1..i]s[i + 1..n]中两者都不是回文串,那么答案就应该为2,如果任意i都满足至少其一是回文串,那么经过讨论可以发现,如果s[1] == s[2],那么s应该是形如aaaaaaaa(全部相同),或者aaabaaa(长度为奇数,只有中间一个不同),如果s[1] != s[2],那么s形如abababa(长度为奇数,两个字母交替的回文串),这三种情况都会是无解,那么我们只需要判断出1(不是回文串),-1(上述三种情况)其他输出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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值