补题的时候忘记关文件读入,T了一晚wdfuck!
甚至把标程拿了对比了运行时间,节约了很多时间。
最后提交比较 标程1500ms,我的150ms,都是用字典树写的。
我也不知道为啥。
#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define rep(i, x, y) for(auto i=(x);i<=(y);++i)
#define dep(i, x, y) for(auto i=(x);i>=(y);--i)
#define gcd(a, b) __gcd(a,b)
const long long mod = 1e9 + 7;
const int maxn = 1e5 + 100;
int lowbit(int x) { return x & -x; }
bool ispow(int n) { return (n & (n - 1)) == 0; }//O(1) 判断是否是 2^k(2的k次方)
int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int n;
int Len;
int nex[1000000][26], cnt = 0;
bool exist[100005]; // 该结点结尾的字符串是否存在
int cot;
int ans[maxn], pos[maxn];
vector<int> mp[maxn];
char s[maxn];
int Q[maxn], qq = 0;
void init() {
for (int i = 0; i < maxn; i++) {
exist[i] = false, pos[i] = -1;
ans[i] = 0;
s[i] = '\0';
mp[i].clear();
for (int j = 0; j < 25; j++)nex[i][j] = 0;
}
cot = 0;
cnt = 0;
qq=0;
}
int deep=0;
inline void insert(char *s, int l) { // 插入字符串
int p = 0;
for (int i = 0; i < l; i++) {
int c = s[i] - 'a';
if (!nex[p][c]) {
nex[p][c] = ++cnt; // 如果没有,就添加结点//
}
p = nex[p][c];
}
exist[p] = true;
mp[p].push_back(++cot);
}
inline void find(char *s, int start) { // 查找字符串
int p = 0;
for (int i = 0; i < 30; i++) {
int c = s[i] - 'a';
if (!nex[p][c]) return;
p = nex[p][c];
if (exist[p] && start > pos[mp[p][0]]) {
pos[mp[p][0]] = start + i;
ans[mp[p][0]]++;
}
}
}
signed main() {
// clock_t start, end;
// start = clock();
// freopen("data.in", "r", stdin);
int T;
scanf("%d",&T);
while (T--) {
init();
scanf("%s", &s);
Len = strlen(s);
cin >> n;
int m = n;
cnt = 0;
while (n--) {
char ss[maxn];
scanf("%s", &ss);
int len = strlen(ss);
insert(ss, len);
}
for (int i = 0; i < Len; i++) {
find(s + i, i);
}
// cout << "%%%%%%%" << qq << "%%%%%%%%%%%%%%%%%%" << endl;
for(int i=0;i<=cnt;i++){
if(exist[i]){
int llen=mp[i].size();
for(int j=1;j<llen;j++)
ans[mp[i][j]]=ans[mp[i][j-1]];
}
}
for (int i = 1; i <= m; i++) {
printf("%d\n", ans[i]);
}
}
// end = clock();
// cout << (double) (end - start) / CLOCKS_PER_SEC << endl;
return 0;
}