Codeforces 1178E

Question is intended: to give you a string of length n containing only a, b, c3 kinds of characters, the string must be different adjacent characters, ask whether there is a sequence of length n / 2 (rounded down) of is a palindrome, there are outputs.

Thinking: adjacent characters must be different, and a total of only three characters, then the string from any two positions selected substring of length 2, one string must be at least two sub-character is the same. Then practice came out. From both ends of the string, each time selecting the leftmost and rightmost two sub-strings, find a character to join the same answer. Finally, if an intermediate region appears length less than 4, the answer to pick a join.

Code:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1000010;
char s[maxn];
bool v[maxn];
int main() {
	scanf("%s", s + 1);
	int n = strlen(s + 1);
	int l, r, ans = 0;
	for (l = 1, r = n - 1; l + 2 <= r; l += 2, r -= 2) {
		for (int j = 0; j < 2; j++)
			for (int k = 0; k < 2; k++) {
				if(s[l + j] == s[r + k]) {
					v[l + j] = v[r + k] = 1;
					ans += 2;
					goto ed;
				}
			}
		ed:
			continue;
	}
	if(ans < n / 2) {
		v[l] = 1;
	}
	for (int i = 1; i <= n; i++) {
		if(v[i] == 1)
			printf("%c", s[i]);
	}
	printf("\n");
} 

  

Guess you like

Origin www.cnblogs.com/pkgunboat/p/11221726.html