64,653
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <set>
#include <string>
#include <cstring>
#include <cstdio>
using namespace std;
struct StrLess
{
bool operator()(const char* a,const char* b)
{
return strcmp(a,b)<0;
}
};
typedef set<const char*,StrLess> StrSet;
int main()
{
char szTemp[32];
char ssDict[10001][32];
int iNum;
cin >> iNum;
StrSet s;
size_t iIdx=0;
while(iNum--)
{
scanf("%s",ssDict[iIdx]);
s.insert(ssDict[iIdx++]);
}
cin >> iNum;
StrSet::iterator end=s.end();
while(iNum--)
{
cin >> szTemp;
const size_t iLen = strlen(szTemp);
int iMatch=0;
for(StrSet::iterator it=s.lower_bound(szTemp); it!=end; ++it)
{
int iCmp = strncmp(*it,szTemp,iLen);
if(iCmp==0)
{
cout << *it << " ";
++iMatch;
}
if(iMatch>7)
break;
}
if(0==iMatch)
cout << szTemp;
cout << endl;
}
}
#include <iostream>
#include <set>
#include <string>
#include <cstring>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
struct StrLess
{
bool operator()(const char* a,const char* b)
{
return strcmp(a,b)<0;
}
};
typedef set<const char*,StrLess> StrSet;
typedef vector<const char*> StrVec;
int main()
{
char szTemp[32];
char ssDict[10001][32];
int iNum;
cin >> iNum;
StrSet s;
size_t iIdx=0;
while(iNum--)
{
scanf("%s",ssDict[iIdx]);
s.insert(ssDict[iIdx++]);
}
cin >> iNum;
StrSet::iterator end=s.end();
StrVec v;
v.reserve(s.size());
for(StrSet::iterator it=s.lower_bound(szTemp); it!=end; ++it)
{
v.push_back(*it);
}
StrVec::iterator vend=v.end();
while(iNum--)
{
cin >> szTemp;
const size_t iLen = strlen(szTemp);
int iMatch=0;
for(StrVec::iterator it=lower_bound(v.begin(),v.end(),szTemp,StrLess()); it!=vend; ++it)
{
int iCmp = strncmp(*it,szTemp,iLen);
if(iCmp==0)
{
cout << *it << " ";//((iMatch!=8)?" ":"");
++iMatch;
}
if(iMatch>7)
break;
}
if(0==iMatch)
cout << szTemp;
cout << endl;
}
}
#include <stdio.h>
#include <string.h>
#include <memory.h>
struct node{
node *next[26];
int flag;
void init() {memset(next,0,sizeof(next));flag=0;}
};
node trien[200005],*root,*p,*rt;
int k,ok,len,num;
char str[21];
void insert(char *str)
{
int i=0,j;
p=root;
while (str[i])
{
j=str[i]-'a';
if (p->next[j]==0)
{
trien[k].init();
p->next[j]=&trien[k++];
}
p=p->next[j];
i++;
}
p->flag=1;
}
node* find(char *str)
{
int i=0,j;
p=root;
while (str[i])
{
j=str[i]-'a';
if (p->next[j]==0)
return 0;
p=p->next[j];
i++;
}
return p;
}
void ps(node *t)
{
int i;
if (num==8)
ok=1;
if (ok)
return;
if (t->flag==1)
{
str[len]='\0';
if (num==0)
printf("%s",str);
else
printf(" %s",str);
num++;
}
for (i=0;i<26;++i)
{
if (t->next[i]!=0)
{
str[len++]='a'+i;
ps(t->next[i]);
if (ok)
return;
}
}
len--;
}
int main()
{
int n,q;
trien[0].init();
root=trien;
k=1;
scanf("%d",&n);
while (n--)
{
scanf("%s",str);
insert(str);
}
scanf("%d",&q);
while (q--)
{
scanf("%s",str);
rt=find(str);
if (rt==0)
printf("%s",str);
else
{
num=ok=0;
len=strlen(str);
ps(rt);
}
printf("\n");
}
return 0;
}