1029-短语搜索
描述
常見文本編輯器的一個功能是搜索,打開一段英文文字,根據一個給定的英文短語,可以搜索得到這個短語在文章中的位置,短語有可能重復出現。現請求出給定的短語在一段文字中出現的最后一個位置。文字中單詞從1開始編號,所求的位置為短語第1個單詞在這段文字中對應單詞的編號。
輸入
多行,每行以 # 為結束,第1行為一段英文文字(單詞數、數字不多于500),其余行是待搜索的英文短語(單詞數不多于10)。這里英文文字、英文短語只包含英文單詞,這些單詞以空格分隔。
輸出
多行,每一行對應輸入中給定的短語在文字中出現的最后一個位置,搜索不到時輸出-1。
樣例輸入
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 #
樣例輸出
1
8
26
51
-1
#include<iostream> using namespace std; int main() {int j,len,len2,flag2;bool flag;int i=0;char str[501][11];char cu[501][11];while(scanf("%s",&str[i])){if(strcmp(str[i],"#")==0)break;elsei++;}len=i;i=0;for(i=0;scanf("%s",&cu[i])!=EOF;i++){if(strcmp(cu[i],"#")==0){len2=i;flag2=-1;for(i=0;i<len;i++){flag=true;for(j=0;j<len2;j++){if(strcmp(str[i+j],cu[j])!=0){flag=false;break;}}if(flag){flag2=i+1;}}cout<<flag2<<endl;i=-1;}}return 0; }
轉載于:https://www.cnblogs.com/Rosanna/p/3436601.html
總結
- 上一篇: HIS系统两种收费模式比较:前计费和后计
- 下一篇: 重写与重载常见问题