菜菜只能靠写简单字符串哈希维持生活。

题目传送门:LOJ #2484

题意简述:

题面讲得很清楚了。

题解:

很显然从两边往中间推,能选的就选上这个贪心策略是对的。

如何判断能不能选上,直接字符串哈希吧。

有一个小细节:中间那块要不要选,即ans要不要加1?判一下串长即可。

#include <cstdio>
#include <cstring>

typedef unsigned long long UL;
const int B = 79;

int T, N;
char str[1000005];

int main() {
	scanf("%d", &T);
	while (T--) {
		scanf("%s", str); N = strlen(str);
		UL s1 = 0, s2 = 0, b = 1;
		int ans = 0;
		for (int i = 0; i < N / 2; ++i) {
			s1 = s1 * B + str[i];
			s2 = s2 + str[N - i - 1] * b;
			b = b * B;
			if (s1 == s2) {
				ans += 2;
				s1 = s2 = 0, b = 1;
			}
		}
		if (N % 2 || s1) ++ans;
		printf("%d\n", ans);
	}
	return 0;
}

相关文章:

  • 2022-01-15
  • 2021-05-21
  • 2022-12-23
  • 2021-06-02
  • 2021-05-28
  • 2021-12-09
  • 2022-03-07
  • 2021-09-21
猜你喜欢
  • 2021-12-24
  • 2022-12-23
  • 2021-10-16
  • 2021-09-20
  • 2021-05-26
  • 2022-01-09
  • 2022-12-23
相关资源
相似解决方案