codeforces 278B New Problem 字串尋找

來源:互聯網
上載者:User
B. New Problemtime 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.

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
5threehorsesgoodsubstringssecretprimematrixbeautifulyear
output
j
input
4aabdefghijklmnopqrstuvwxyzc
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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.