VMSUBSTR - Vườn cây của ba
Tác giả: flashmt
Ngôn ngữ: C++
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n, Q, id[256], flag[256], cnt[55][55];
char s[1000100], t[100];
int main()
{
for (char c = 'a'; c <= 'z'; c++) id[c] = c - 'a';
for (char c = 'A'; c <= 'Z'; c++) id[c] = c - 'A' + 26;
scanf("%d%s", &n, s);
for (int i = 1; i < n; i++)
{
int x = id[s[i - 1]], y = id[s[i]];
if (x > y) swap(x, y);
cnt[x][y]++;
}
scanf("%d", &Q);
for (int iq = 1; iq <= Q; iq++)
{
scanf("%s", t);
int len = strlen(t);
for (int i = 0; i < len; i++) flag[id[t[i]]] = iq;
int ans = 0;
for (int i = 0; i < 52; i++)
for (int j = i + 1; j < 52; j++)
if ((flag[i] == iq) ^ (flag[j] == iq))
ans += cnt[i][j];
if (flag[id[s[0]]] == iq) ans = ans / 2 + 1;
else ans = (ans + 1) / 2;
printf("%d\n", ans);
}
}