数据结构---栈(STL)

codevs1051接龙游戏

第一次用STL写了写“栈”

表示还需要再多看看理解理解。

 1 #include<iostream>
 2 #include<stack>//STL栈
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 int main()
 7 {
 8     string A[100005];//二维 
 9     int n;
10     cin>>n;
11     for(int i=0;i<n;i++)
12     cin>>A[i];//二维字符串  这下懂了吧
13     int ans=1,maxn=1;
14     sort(A,A+n);//直接按字典序排。。。! 
15     /*!stack<int>s*/
16     stack<string>sta;//
17     sta.push(A[0]);//sta[1]=A[0];
18     for(int i=1;i<n;i++)
19     {
20         string New=A[i];
21         bool F=true;
22         while(1)
23         {
24             F=true;
25             if(sta.empty())
26             {
27                 sta.push(New);
28                 ans=1;
29                 break;
30             }
31             else
32             {
33                 string top=sta.top();//top   ...top这样也可以  好吧
34                 int num=top.size();//(string)  size==strlen 
35                 for(int i=0;i<num;i++)  //new的长度>=top的长度 
36                 {
37                     //F&=top[i]==New[i];
38                     if(top[i]!=New[i]) F=false;  
39                 }
40                 if(F&&New!=top)//two words are jielong.&&two words are different.
41                 {
42                     sta.push(New);
43                     ans++;
44                     break;
45                 }
46                 else{
47                     sta.pop();
48                     ans--;
49                 }
50             }
51         }
52         maxn=max(maxn,ans);
53     }
54     cout<<maxn<<endl;
55     return 0;
56  } 

 

 

posted on 2016-03-16 00:38  babyyang  阅读(201)  评论(0编辑  收藏  举报