HDU 1238

 1 #include <iostream>
 2 #include <cstring>
 3 #include <string>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 const int INF = 1e6;
 8 
 9 vector<string> a;
10 string suba;
11 string rsuba;
12 
13 int main(void)
14 {
15     int cas;
16     cin >> cas;
17     while (cas--)
18     {
19         int n;
20         cin >> n;
21         for (int i = 0; i < n; ++i)
22         {
23             string temp;
24             cin >> temp;
25             for (int i = 0; i < temp.size(); ++i)
26                 temp[i] = tolower(temp[i]);
27             a.push_back(temp);
28         }
29 
30         int p;
31         int minSize = INF;
32         for (int i = 0; i < n; ++i)
33         {
34             if (a[i].size() < minSize)
35             {
36                 minSize = a[i].size();
37                 p = i;
38             }
39         }
40 
41         int result = 0;
42         for (int i = 0; i < minSize; ++i)
43         {
44             for (int j = 1; i + j <= minSize; ++j)
45             {
46                 suba = a[p].substr(i, j);
47                 rsuba = suba;
48                 reverse(rsuba.begin(), rsuba.end());
49                 int k;
50                 for (k = 0; k < n; ++k)
51                 {
52                     if (k != p)
53                     {
54                         if (a[k].find(suba) == -1 && a[k].find(rsuba, 0) == -1)
55                             break;
56                     }
57                 }
58                 if (k == n)// match the case
59                 {
60                     if (j > result)
61                         result = j;
62                 }
63             }
64         }
65         cout << result << endl;
66         a.clear();
67     }
68 
69     return 0;
70 }

猜你喜欢

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