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;
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/scar_lyw/article/details/78960526
文章标签: 省选 结论分析
个人分类: NOIP解题报告 暴力
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭