HDU 2328 Corporate Identity

 1 #include <iostream>
 2 #include <cstring>
 3 #include <string>
 4 #include <vector>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 const int MaxSize = 210;
 9 
10 vector<string> a;
11 string suba;
12 int index;//find the smallest to be the pattern string
13 
14 int main()
15 {
16     while (true)
17     {
18         int n;
19         cin >> n;
20         if (n == 0)
21             break;
22         for (int i = 0; i < n; ++i)
23         {
24             string temp;
25             cin >> temp;
26             a.push_back(temp);
27         }
28 
29         //find the smallest to be the pattern string
30         int Min = MaxSize;
31         for (int i = 0; i < a.size(); ++i)
32         {
33             if (a[i].size() < Min)
34             {
35                 index = i;
36                 Min = a[i].size();
37             }
38         }
39 
40         string res = "";
41         int Len = a[index].size();//the len of pattern string
42         for (int i = 0; i < Len; ++i)//start pos
43         {
44             for (int j = 1; i + j <= Len; ++j)//Len of substr
45             {
46                 suba = a[index].substr(i, j);
47                 int k;
48                 for (k = 0; k < a.size(); ++k)
49                 {
50                     if (k == index)
51                         continue;
52                     if (a[k].find(suba) == -1)
53                         break;
54                 }
55                 if (k == a.size())//match condition
56                 {
57                     if (res.size() < suba.size())
58                         res = suba;
59                     else if (res.size() == suba.size() && res > suba)
60                         res = suba;
61                 }
62             }
63         }
64         if (res != "")
65             cout << res << endl;
66         else
67             cout << "IDENTITY LOST" << endl;
68 
69         a.clear();
70     }
71 
72     return 0;
73 }

猜你喜欢

转载自www.cnblogs.com/ducklu/p/9010527.html