NC20508. [ZJOI2013]丽洁体
描述
输入描述
包含4行。
第一行是某个也许不规范的体作品T,
接下来三行分别代表A, B, C。
1 ≤ |T|, |A|, |B|, |C| ≤ 50000;
所有单词长度不超过5,出现次数不超过500;数据保证答案总存在
输出描述
仅一行,包含一个数,即最少的去除单词数。
示例1
输入:
xiang yao yi zhi ai zhe mou wu de hua yi yao guai zhi si lai shuo tai chang le xiang yao shi xian yi qie meng xiang de hua yi ren lei zhi sheng lai shuo tai duan le yao tai chang le yao tai duan le
输出:
2
说明:
【样例说明】C++11(clang++ 3.9) 解法, 执行用时: 11ms, 内存消耗: 1100K, 提交时间: 2020-05-22 16:02:20
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define For(i, a, b) for (i = a; i <= b; i++) #define Rof(i, a, b) for (i = a; i >= b; i--) using namespace std; const int N = 3e5 + 5, INF = 0x3f3f3f3f; int L, R, nt, na, nb, nc, t[N], a[N], b[N], c[N], ans; char T[N], A[N], B[N], C[N]; void __hash(char *s, int *rp, int &n, int len) { int i; For (i, 0, len - 1) if (s[i] < 'a' || s[i] > 'z') continue; else { if (i == 0 || s[i - 1] < 'a' || s[i - 1] > 'z') rp[++n] = 0; rp[n] = rp[n] * 27 + s[i] - 'a' + 1; } } int main() { int i, j, th = 1; gets(T); gets(A); gets(B); gets(C); __hash(T, t, nt, strlen(T)); __hash(A, a, na, strlen(A)); __hash(B, b, nb, strlen(B)); __hash(C, c, nc, strlen(C)); For (L, 1, nt) if (t[L] == a[th]) { if (th == na) {L++; break;} else th++; } else ans++; th = nc; Rof (R, nt, 1) if (t[R] == c[th]) { if (th == 1) {R--; break;} else th--; } else ans++; int minp = INF; For (i, L, R) { if (t[i] != b[1]) continue; th = 0; int mp = 0; For (j, i, R) if (t[j] == b[th + 1]) { th++; if (th == nb) break; } else mp++; if (th == nb) minp = min(minp, mp); } cout << ans + minp << endl; return 0; }