本文共 833 字,大约阅读时间需要 2 分钟。
一看到此题就觉得是一个二分
但由于 s t r i n g string string不好处理,所以又用了个 m a p map map.
m a p map map是个很实用的东西,我也不太会
所以有不懂的请问
**#include<algorithm>#include<iostream>#include<cstdio>#include<cmath>#include<map> //头文件要加using namespace std;int w,n,wh,j,k,l,r,mid;string s[30001],fi;map<string,int>cha; //定义mapint main(){ freopen("auto.in","r",stdin); freopen("auto.out","w",stdout); cin>>w>>n; for(int i=1; i<=w; i++) { cin>>s[i]; cha[s[i]]=i; } sort(s+1,s+1+w); for(int i=1; i<=n; i++) { cin>>wh>>fi; k=0,l=1,r=w; while(l<=r) //进行二分 { mid=(l+r)/2; if(s[mid]<fi) l=mid+1; else r=mid-1; } if(l==0||l+wh-1>w||s[l+wh-1].find(fi,0)) //实用的map cout<<-1<<endl; else if(s[l+wh-1].find(fi,0)==0) cout<<cha[s[l+wh-1]]<<endl; } return 0;}**
转载地址:http://xjle.baihongyu.com/