您的位置:首页 > 其它

短语搜索

2015-07-18 09:04 609 查看
Description

常见文本编辑器的一个功能是搜索,打开一段英文文字,根据一个给定的英文短语,可以搜索得到这个短语在文章中的位置,短语有可能重复出现。现请求出给定的短语在一段文字中出现的最后一个位置。文字中单词从1开始编号,所求的位置为短语第1个单词在这段文字中对应单词的编号。

Input

多行,每行以 # 为结束,第1行为一段英文文字(单词数、数字不多于500),其余行是待搜索的英文短语(单词数不多于10)。这里英文文字、英文短语只包含英文单词,这些单词以空格分隔。

Output

多行,每一行对应输入中给定的短语在文字中出现的最后一个位置,搜索不到时输出-1。

Sample Input

STOCKHOLM April 21 PRNewswire FirstCall Students from St Petersburg State University of IT Mechanics and Optics are crowned the 2009 ACM International Collegiate Programming Contest World Champions in the Stockholm Concert Hall where the Nobel Prizes are presented every year Sponsored by IBM the competition took place today at KTH the Royal Institute of Technology #

STOCKHOLM #

St Petersburg State University of IT Mechanics and Optics #

World Champions #

the #

NUPT #

Sample Output

1

8

26

51

-1

#include<stdio.h>
#include<string.h>
int main()
{
char a[500][50],b[500][50];
int i=0;
scanf("%s",a[0]);
while(a[i++][0]!='#')
scanf("%s",a[i]);
while(scanf("%s",b[0])!=EOF)
{
int j=0,t,y,m,g=0;
while(b[j++][0]!='#')
scanf("%s",b[j]);
for(t=0;t<i-1;t++)
if(strcmp(b[0],a[t])==0)
{
int ok=1;
for(y=1,m=t+1;y<j-1&&m<i-1;y++,m++)
if(strcmp(b[y],a[m])!=0)
{ok=0;break;}
if(ok)
g=t+1;
}
if(g)
printf("%d\n",g);
else
printf("-1\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: