Codeforces Round #170 (Div. 2) B. New Problem(好题)

B. New Problem
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Coming up with a new problem isn't as easy as many people think. Sometimes it is hard enough to name it. We'll consider a title original if it doesn't occur as a substring in any titles of recent Codeforces problems.

You've got the titles of n last problems — the strings, consisting of lowercase English letters. Your task is to find the shortest original title for the new problem. If there are multiple such titles, choose the lexicographically minimum one. Note, that title of the problem can't be an empty string.

A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (where |s| is the length of string s) is string slsl + 1... sr.

String x = x1x2... xp is lexicographically smaller than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there exists such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The string characters are compared by their ASCII codes.

Input

The first line contains integer n (1 ≤ n ≤ 30) — the number of titles you've got to consider. Then follow n problem titles, one per line. Each title only consists of lowercase English letters (specifically, it doesn't contain any spaces) and has the length from 1 to 20, inclusive.

Output

Print a string, consisting of lowercase English letters — the lexicographically minimum shortest original title.

Sample test(s)
Input
5
threehorses
goodsubstrings
secret
primematrix
beautifulyear
Output
j
Input
4
aa
bdefghijklmn
opqrstuvwxyz
c
Output
ab
Note

In the first sample the first 9 letters of the English alphabet (a, b, c, d, e, f, g, h, i) occur in the problem titles, so the answer is letter j.

In the second sample the titles contain 26 English letters, so the shortest original title cannot have length 1. Title aa occurs as a substring in the first title.

想了各种数据结构去做,看了大神的思路才知道自己弱爆了……明明暴力就能轻松解决……

The total number of different strings of 2 letters is 262 = 676, but the total length of the input strings is no more than 600. It means that the length of answer is no more than 2. So just check all the strings of length 1 and 2.

 1 #include <iostream>
 2 #include <string>
 3 #include <set>
 4 #include <map>
 5 #include <vector>
 6 #include <stack>
 7 #include <queue>
 8 #include <cmath>
 9 #include <cstdio>
10 #include <cstring>
11 #include <algorithm>
12 using namespace std;
13 #define LL long long
14 #define cti const int
15 #define ctll const long long
16 #define dg(i) cout << "*" << i << endl;
17 
18 bool ha[2000];
19 char s[31][21];
20 int Hash(char a, char b)
21 {
22     return (a - 'a') * 50 + b - 'a' + 26;
23 }
24 
25 int main()
26 {
27     int n;
28     while(scanf("%d", &n) != EOF)
29     {
30         memset(ha, false, sizeof(ha));
31         while(n--)
32         {
33             scanf("%s", s[n]);
34             for(int i = 0; s[n][i] != '\0'; i++)
35             {
36                 ha[s[n][i]-'a'] = true;
37                 if(s[n][i+1] != '\0') ha[Hash(s[n][i], s[n][i+1])] = true;
38             }
39         }
40         int ok = 0;
41         for(int i = 0; i < 26; i++)
42             if(!ha[i])
43             {
44                 printf("%c\n", i + 'a');
45                 ok = 1;
46                 break;
47             }
48         if(!ok)
49         {
50             char x, y;
51             for(x = 'a'; x <= 'z' && !ok; x++)
52             {
53                 for(y = 'a'; y <= 'z' && !ok; y++)
54                 {
55                     if(!ha[Hash(x, y)])
56                         ok = 1;
57                 }
58             }
59             printf("%c%c\n", x - 1, y - 1);
60         }
61     }
62     return 0;
63 }
原文地址:https://www.cnblogs.com/cszlg/p/2940492.html