WA_automat @ 2022-07-18 15:03:54
不知道是不是被卡常数了 TLE了两个点
#include<iostream>
#include<string>
using namespace std;
const int N = 3e6 + 1;
int t, n, q, son[N][62], cnt[N], idx;
string str;
inline int getnum(char x) {
if (x >= 'A' && x <= 'Z')
return x - 'A';
else if (x >= 'a' && x <= 'z')
return x - 'a' + 26;
else
return x - '0' + 52;
}
inline void insert(const char* str) {
int p = 0;
for (register int i = 0; str[i]; ++i) {
int u = getnum(str[i]);
if (!son[p][u]) son[p][u] = ++idx;
p = son[p][u];
++cnt[p];
}
}
int query(const char* str) {
int p = 0;
for (register int i = 0; str[i]; i++)
{
int u = getnum(str[i]);
if (!son[p][u]) return 0;
p = son[p][u];
}
return cnt[p];
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> t;
while (t--) {
for (register int i = 0; i <= idx; i++)
for (int j = 0; j <= 62; j++)
son[i][j] = 0;
for (register int i = 0; i <= idx; i++)
cnt[i] = 0;
cin >> n >> q;
while (n--) {
cin >> str;
insert(str.c_str());
}
while (q--) {
cin >> str;
cout << query(str.c_str()) << '\n';
}
}
return 0;
}
by WA_automat @ 2022-07-27 23:44:42
已过,AC代码如下:
#include<iostream>
#include<string>
using namespace std;
const int N = 3e6 + 10;
int t, n, q, son[N][65], cnt[N], idx;
char str[N];
int getnum(char x) {
if (x >= 'A' && x <= 'Z')
return x - 'A';
else if (x >= 'a' && x <= 'z')
return x - 'a' + 26;
else
return x - '0' + 52;
}
void insert(const char* str) {
int p = 0;
for (int i = 0; str[i]; ++i) {
int u = getnum(str[i]);
if (!son[p][u]) son[p][u] = ++idx;
p = son[p][u];
++cnt[p];
}
}
int query(const char* str) {
int p = 0;
for (int i = 0; str[i]; i++) {
int u = getnum(str[i]);
if (!son[p][u]) return 0;
p = son[p][u];
}
return cnt[p];
}
int main(void) {
scanf("%d", &t);
while (t--) {
for (int i = 0; i <= idx; i++)
for (int j = 0; j <= 122; j++)
son[i][j] = 0;
for (int i = 0; i <= idx; i++)
cnt[i] = 0;
idx = 0;
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++) {
scanf("%s", str);
insert(str);
}
for (int i = 1; i <= q; i++) {
scanf("%s", str);
printf("%d\n", query(str));
}
}
return 0;
}