1414. Astronomical Database(STL)

1414

破题 又逼着用stl 卡内存 trie树太耗了 水不过去

用set存字符串 set可以自己按一定顺序存 且没有重复的 再用lower_bound二分查找字符串的第一次出现 接着往后找就行了

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<stdlib.h>
 5 #include<algorithm>
 6 #include<set>
 7 #include<string>
 8 using namespace std;
 9 #define N 10010
10 set<string>q;
11 set<string>::iterator it;
12 int kk;
13 string ys = "sun";
14 char ss[22];
15 bool cmp(string s)
16 {
17     int i,tk = (int)s.size();
18     if(tk<kk)
19     return false;
20     for(i = 0 ; i < kk ; i++)
21     {
22         if(s[i]!=ss[i])
23         return false;
24     }
25     return true;
26 }
27 int main()
28 {
29     char c;
30     q.insert(ys);
31     while(cin>>c)
32     {
33         cin>>ss;
34         if(c=='?')
35         {
36             cout<<ss<<endl;
37             kk = strlen(ss);
38             if(q.size())
39             {
40                 it = lower_bound(q.begin(),q.end(),ss);
41                 int k;
42                 for(k=0; it!=q.end()&&cmp(*it)&&k<20;it++,k++)
43                 cout<<"  "<<(*it)<<endl;
44             }
45         }
46         else
47         {
48             q.insert(ss);
49         }
50         getchar();
51     }
52     return 0;
53 }
View Code

 

posted @ 2013-10-02 23:40  _雨  阅读(242)  评论(0编辑  收藏  举报