ivan11 @ 2024-10-22 21:04:54
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int T;
int n,m;
int idx;
int cnt[3000005];
int next[3000005][65];
struct Trie
{
void clear()
{
idx = 0;
memset(cnt, 0, sizeof cnt);
memset(next, 0, sizeof next);
}
int getnum(char c)
{
if('A' <= c && c <= 'Z')
{
return c - 'A';
}
else if()
}
void insert(string a)
{
int p = 0;
for(int i=0;i<a.size();++i)
{
int k = getnum(a[i]);
if(next[p][k] == 0)
{
next[p][k] = ++idx;
}
p = next[p][k];
++cnt[p];
}
return ;
}
bool query(string a)
{
int p = 0;
for(int i=0;i<a.size();++i)
{
int k = getnum(a[i]);
if(next[p][k] == 0)
{
return 0;
}
p = next[p][k];
}
return cnt[p];
}
} q;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin>>T; ++T;
while(--T)
{
q.clear();
cin>>n>>m;
for(int i=1;i<=n;++i)
{
string a;
cin>>a;
q.insert(a);
}
for(int i=1;i<=m;++i)
{
string a;
cin>>a;
cout<<q.query(a)<<"\n";
}
}
return 0;
}
by ronkeyson @ 2024-10-23 00:01:27
在解答的同时,谢谢你的程序,让我深入巩固了这道题鸭~
总结错误点:
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;
}
}
接下来撮合撮合就搞定啦~
提交
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int T;
int n,m;
int idx;
int cnt[3000005];
int nxt[3000005][65];
struct Trie
{
void clear()
{
for (int i = 0; i <= idx; i++) {
cnt[i] = 0;
for (int j = 0; j < 65; j++) {
nxt[i][j] = 0;
}
}
idx = 0;
}
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(string a)
{
int p = 0;
for(int i=0;i<a.size();++i)
{
int k = getnum(a[i]);
if(nxt[p][k] == 0)
{
nxt[p][k] = ++idx;
}
p = nxt[p][k];
++cnt[p];
}
return ;
}
int query(string a)
{
int p = 0;
for(int i=0;i<a.size();++i)
{
int k = getnum(a[i]);
if(nxt[p][k] == 0)
{
return 0;
}
p = nxt[p][k];
}
return cnt[p];
}
} q;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin>>T; ++T;
while(--T)
{
q.clear();
cin>>n>>m;
for(int i=1;i<=n;++i)
{
string a;
cin>>a;
q.insert(a);
}
for(int i=1;i<=m;++i)
{
string a;
cin>>a;
cout<<q.query(a)<<"\n";
}
}
return 0;
}
by ivan11 @ 2024-10-23 08:55:45
@ronkeyson 谢谢!!!
by ronkeyson @ 2024-10-23 21:25:26
不客气鸭~