给定 n 个字符串,求有多少字符串是其他字符串的前缀。

 

 

 1 #include <cstdio>
 2 #include <set>
 3 #include <iostream>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 
 8 
 9 int main()
10 {
11     int n, i, cnt = 0;int t = 1;
12     string strs[1010], ms;
13     set <string> s;
14     set <string> s2;
15     cin>>n;
16     for (i = 0; i < n; i++)
17         cin >> strs[i];
18     for (i = 0; i < n; i++) 
19         for (int x = i + 1; x < n; x++) 
20         {
21             if (strs[i] == strs[x])
22                 s2.insert(strs[i]);
23             break;
24         }
25         
26     for (i = 0; i < n; i++)
27         for (t = 1; t < strs[i].length(); t++) 
28         {
29             ms = strs[i].substr(0, t);
30             s.insert(ms);
31         }
32         
33     for (i = 0; i < n; i++) 
34     { 
35         if (s.count(strs[i]) != 0)
36             cnt++;
37         if(s2.count(strs[i]) != 0)
38             cnt++;
39     }
40     printf("%d", cnt);
41     return 0;
42 }

 

posted @ 2020-09-30 10:29  然终酒肆  阅读(246)  评论(0编辑  收藏  举报