Hdu 4909 String (count)
Link: hdu 4909 String
Given a string consisting of lowercase letters, it can contain at most one question mark. A question mark can indicate null or any letter. Ask how many substrings are there, and the number of occurrences of letters is an even number.
Solution: because up to 26 letters correspond to the odd number of each letter, 1 indicates the even number, and 0 indicates a prefix string as a binary number. Then, for each of the same number s, either of them is a feasible substring (combined mathematics ). then, if there is a question mark, enumerate the replacement characters of the question mark, and then add this character to the status after the question mark. In this case, we will discuss the numbers separately.
This question is handed over to C ++, and the result is stuck to the FST. I think it is not scientific. I only need to add the sorting complexity (nlogn), and then it will be called G ++, however, it seems that a 1 <26 array is used to calculate the number, and the space limit is too loose.
#include
#include
#include using namespace std;typedef int ll;const int maxn = 20005;int n, num[maxn], s[maxn];char str[maxn];ll solve () { int tmp = num[0] = 0; for (int i = 1; i <= n; i++) { if (str[i] != '?') tmp ^= (1<<(str[i]-'a')); s[i] = num[i] = tmp; } ll ret = 0; sort(num, num + n + 1); int mv = 0; while (mv <= n) { int p = 0; while (p + mv <= n && num[p+mv] == num[mv]) p++; ret += p * (p-1) / 2; mv += p; } return ret;}int a[maxn], b[maxn];ll handle (int x, int pos) { int A = pos, B = n + 1 - pos; for (int i = 0; i < pos; i++) a[i] = s[i]; for (int i = pos; i <= n; i++) b[i-pos] = s[i]^(1<
b[mvb]) mvb++; else if (a[mva] < b[mvb]) mva++; else { int i = 0, j = 0; while (i + mva < A && a[i+mva] == a[mva]) i++; while (j + mvb < B && b[j+mvb] == b[mvb]) j++; ret += i * j; mva += i; mvb += j; } } return ret;}int main () { int cas; scanf("%d", &cas); while (cas--) { scanf("%s", str+1); n = strlen(str+1); int pos = -1; for (int i = 1; i <= n; i++) { if (str[i] == '?') { pos = i; break; } } ll ans = solve(); if (pos != -1) { for (int i = 0; i + 'a' <= 'z'; i++) ans += handle(i, pos); } printf("%d\n", ans); } return 0;}