标签:io os ar for sp amp ef new ios
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<iostream>
using namespace std;
typedef struct tt
{
struct tt *next[26];
int num;
}tire;
void insert(tire *p,string temp)
{
int i=0,j,k;
tire *n=p,*q;
while(temp[i])
{
j=temp[i]-‘a‘+1;
if(n->next[j]==NULL)
{
q=new tire;
q->num=0;
for(k=0;k<=25;k++)
q->next[k]=NULL;
n->next[j]=q;
}
n->next[j]->num++;
n=n->next[j];
i++;
}
}
void myfree(tire *p){
for(int i = 0; i <= 25; i++){
if(p->next[i] != NULL){
myfree(p->next[i]);
}
}
delete p;
return ;
}
int getnode(tire *p,string temp)
{
int i=0,j;
tire *n=p;
while(temp[i])
{
j=temp[i]-‘a‘+1;
if(n->next[j]==NULL)
{
return 0;
}
else
{
n=n->next[j];
}
i++;
}
return n->num;
}
int main()
{
int i;
tire * root=new tire;
root->num=0;
for(i=0;i<=25;i++)
root->next[i]=NULL;
int n;
string temp;
//char temp[11];
//scanf("%d",&n);
cin>>n;
while(n--)
{
cin>>temp;
// scanf("%s",temp);
insert(root,temp);
}
cin>>n;
// scanf("%d",&n);
while(n--)
{
cin>>temp;
// scanf("%s",temp);
cout<<getnode(root,temp)<<endl;
//printf("%d\n",getnode(root,temp));
}
myfree(root);
return 0;
}
标签:io os ar for sp amp ef new ios
原文地址:http://www.cnblogs.com/notlate/p/4030135.html