编程之美--最短摘要
void findtext(char **pstr,int len,char **pkey,int l)
{
int nstart=0,nend=0;
int mlen=10000,mstart=0,mend=len-1;
bool check=0;
while(1)
{
while(!isin(pstr,len,nstart,nend,pkey,l)&&(nend<len))
{
nend++;
}
while(isin(pstr,len,nstart,nend,pkey,l))
{
int tmplen=nend-nstart+1;
check=1;
if(tmplen<mlen)
{
mlen=tmplen;
mstart=nstart;
mend=nend;
}
nstart++;
}
if(nend>=len) break;
}
if(check==1)
{
for(int i=mstart;i<=mend;i++)
cout<<pstr[i]<<" ";
cout<<endl;
}
else cout<<"not exist"<<endl;
}
bool isin(char **pstr,int len,int start,int end,char **pkey,int l)
{
if(start>end||end>=len||end-start+1<l) return false;
bool visit[1000];
memset(visit,0,sizeof(visit));
for(int i=0;i<l;i++)
{
int j;
for( j=start;(j<=end);j++)
{
if(visit[j-start]==0)
{
int ret=strcmp(pkey[i],pstr[j]);
if(ret==0)
{
visit[j-start]=1;
break;
}
}
}
if(j>end) return false;
}
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
char **pstr=new char*[100];
for(int i=0;i<100;i++)
{
pstr[i]=new char[1000];
}
char **pkey=new char*[100];
for(int i=0;i<100;i++)
{
pkey[i]=new char[1000];
}
int n,m;
while(cin>>n>>m)
{
for(int i=0;i<n;i++)
cin>>pstr[i];
for(int i=0;i<m;i++)
cin>>pkey[i];
findtext(pstr,n,pkey,m);
}
system("pause");
return 0;
}