codeforces 278B 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 titleoriginal 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.

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.

感受: 这道题当时看到了觉得好复杂~~~  看了解题报告后发现自己进入了思维的误区~~

题意: 从给出的n个字符串中找到第一次没有出现的字串(先从字串的长度1开始遍历a-z),然后从字串的长度2开始,遍历(aa-zz)然后就结束了~~

为什么遍历到zz就结束了呢,因为最多30个字串每个字串的长度为20,然后最长也就600个字符,然后连续的两两一组超不过aa-zz的全部组合,然后由aa-zz 总共为26*26=676个了 也就是出先不了3个字符的字串~~~汗~~就是两个以内一定能找到。

然后正确的选用库函数也是其中的关键的一环,查找用strchr和strstr来查找吧,有简单又有效率~~

post code:

#include<stdio.h>
#include<string.h>
char a[40][30];
char ch;
char ans[4];
int n;
int find1()
{

   for(ch='a';ch<='z';ch++){
     int judge=0;        
     for(int i=1;i<=n;i++)
       if(strchr(a[i],ch)!=NULL) judge=1; //找到字符 
     if(judge==0)return 1;      // 没要找到字符 输出答案   
   }
   return 0;
}
int find2()
{
    ans[0]='a';
    while(ans[0]<='z'){
        for(ans[1]='a';ans[1]<='z';ans[1]++){ 
           int judge=0; 
           for(int i=1;i<=n;i++)
              if(strstr(a[i],ans)!=NULL) judge=1;
           if(judge==0)return 1;
        } 
        ans[0]++; 
    }
    return 0;
}


int main()
{
    while(scanf("%d",&n)!=EOF){
         int flag=0;
         int i;
         for(i=1;i<=n;i++)
            scanf("%s",a[i]);
         flag=find1(); //查找字符串的长度为1的
         if(flag==1){
            printf("%c\n",ch);continue; 
         } 
         flag=find2(); //查找字符串的长度为2的
         if(flag==1){
            printf("%s\n",ans);continue;    
         }         
    }
   return 0;
}





你可能感兴趣的:(算法)