1051 X-Dragon games and 1051 X-Dragon games
1051 Solitaire games
Time Limit: 1 s space limit: 128000 KB title level: Diamond Title Description
Description
N words are given and sorted by length. If a word I is the prefix of a word j, I-> j is counted as a solitaire (two identical words cannot be counted as a solitaire ).
Your task is to find the longest dragon for the input words.
Input description
Input Description
First Act N (1 <=n <= 105 ). Each of the following N rows contains one word (in lowercase), which is sorted by length. (Length of each word <50)
Output description
Output Description
The maximum length of a dragon.
Sample Input
Sample Input
5
I
A
Int
Able
Inter
Sample output
Sample Output
3
Data range and prompt
Data Size & Hint
1 <= N <= 105
1 #include<iostream> 2 #include<cstdio> 3 #include<stack> 4 #include<cstring> 5 #include<algorithm> 6 using namespace std; 7 string a[1000001]; 8 int tot=1; 9 stack<int>s;10 int main()11 {12 int n;13 scanf("%d",&n);14 for(int i=1;i<=n;i++)15 {16 //scanf("%s",a[i]);17 cin>>a[i];18 }19 sort(a+1,a+n+1);20 s.push(1);21 for(int i=2;i<=n;i++)22 {23 while(!s.empty())24 {25 int t=s.top();26 int flag=0;27 if(a[t].size()<a[i].size())28 {29 if(a[i].find(a[t])==0)30 {31 break;32 }33 else34 {35 flag=1;36 }37 }38 else flag=1;39 if(flag==0)40 break;41 if(flag==1)42 {43 s.pop();44 }45 }46 s.push(i);47 if(s.size()>tot)48 tot=s.size();49 }50 cout<<tot;51 return 0;52 }