[不知道哪来的题] 串(string)

Description

给定一个由小写字母组成的字符串 (s) ,每次你可以删去它的一个非回文子串,求删成空串的最小次数。

Input

第一行一个整数 (t(tle 20)) 表示数据组数。
每组数据第一行一个整数 (nle 10^5) 表示字符串长度, 第二行一个字符串 (s)

Output

每组数据输出一行一个整数表示答案, 如果无法删成空串输出 (-1)

Sample

Sample Input

2
7
abcdcba
3
xxx

Sample Output

2
-1

Solution

这题就水了啊...

(forall iin [1,n])(s[1cdots i])(s[i+1cdots n]) 至少有一个是回文串,则该串无解。所以无解就三种情况:

  1. aaaaaaa
  2. aaabaaa
  3. abababa

排除掉这三种,如果原串不是回文串答案就是 (1) ,否则就是 (2)

至于证明,wxh010910: There's no proof in OI, 我不会。

#include<bits/stdc++.h>
using namespace std;

#define N 100001
#define rep(i, a, b) for (int i = a; i <= b; i++)

int n;
char s[N];

int main() {
	int Case; scanf("%d", &Case);
	while (Case--) {
		scanf("%d%s", &n, s + 1);
		bool tag = 0, tag1 = 0, tag2 = 0, tag3 = 0;
		int mid = (n >> 1) + 1;
		rep(i, 1, mid) if (s[i] != s[n - i + 1]) { tag = 1; break; }
		if (tag) { puts("1"); continue; }
        rep(i, 1, n) tag1 |= (i > 1 && s[i] != s[i - 1]), tag2 |= (i > 2 && s[i] != s[i - 2]);
        rep(i, 2, mid - 1) tag3 |= (s[i] != s[i - 1]);
        rep(i, mid + 2, n) tag3 |= (s[i] != s[i - 1]);
        puts(tag1 && tag2 && tag3 ? "2" : "-1");
	}
	return 0;
}
原文地址:https://www.cnblogs.com/aziint/p/9160625.html