有点想不通这个点名的字符串是学生姓名前缀时怎么才能让它输出WRONG。
代码+注释:
#include<bits/stdc++.h>
using namespace std;
int cnt=1;
struct trie{
int son[26];//存储的子节点的编号
bool rep;//是否访问过
int num;//访问次数
}t[5000005];
int n,m;
string s;
void ins(string s){
int now=0;
for(int i=0;i<s.size();i++){//遍历字符串
int ch=s[i]-'a';
if(t[now].son[ch]==0){//如果未被存储过
t[now].son[ch]=cnt++;
}
now=t[now].son[ch];//继续向下搜
t[now].num++;//遍历次数++
}
}
int ind(string s){
int now=0;
for(int i=0;i<s.size();i++){
int ch=s[i]-'a';
if(t[now].son[ch]==0)//没有这个字符
return 3;//错误
now=t[now].son[ch];//继续搜
}
if(t[now].num==0){//没在ins里遍历过
return 3;
}
if(t[now].rep==false){
t[now].rep=true;
return 1;//正确且第一次出现
}
return 2;//正确但不止一次出现
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>s;
ins(s);
}
cin>>m;
for(int i=1;i<=m;i++){
cin>>s;
int r=ind(s);
if(r==1){
cout<<"OK\n";
}else if(r==2){
cout<<"REPEAT\n";
}else{
cout<<"WRONG\n";
}
}
return 0;
}
P2580