hihocoder 1152 Lucky Substrings
#1152 : Lucky Substrings
時間限制:10000ms 單點時限:1000ms 內(nèi)存限制:256MB描述
A string s is?LUCKY?if and only if the number of different characters in s is a?fibonacci number. Given a string consisting of only lower case letters, output all its lucky non-empty substrings in lexicographical order. Same substrings should be printed once.
輸入
A string consisting no more than 100 lower case letters.
輸出
Output the lucky substrings in lexicographical order, one per line. Same substrings should be printed once.
- 樣例輸入
-
aabcd樣例輸出 -
a aa aab aabc ab abc b bc bcd c cd d
題目大意:給定一個只包含小寫字母的字符串S。對于S的任意一個非空子串,若其包含的不同字母個數(shù)為fibonacci數(shù)列中的數(shù),
則我們認(rèn)為這個子串為幸運的。請找出S的所有幸運的子串。不要重復(fù)輸出。解題思路
一個簡單的解題思路是直接枚舉S的所有子串,并對其不同字母個數(shù)進行統(tǒng)計。
S均由小寫字母組成,因此其包含的不同字母個數(shù)最多為26個。而在26以內(nèi)且屬于fibonacci數(shù)列的數(shù)為1,2,3,5,8,13,21。因此只有當(dāng)子串中不同字母的個數(shù)為1,2,3,5,8,13,21時,該子串才是幸運的。
接下來即是如何統(tǒng)計一個子串的不同字母個數(shù),下面給出一種比較樸素的方法:
isLucky(subString):alphabet[] = falsecount = 0For c in subStringIf not alphabet[c] Thenalphabet[c] = truecount = count + 1End IfEnd ForReturn (count is Fibonaccid number)S的最大長度為?N?= 100,該樸素算法的時間復(fù)雜度為O(N^3),是可以通過所有數(shù)據(jù)的。
同時,我們可以通過一個小的優(yōu)化,將算法的時間復(fù)雜度減少的O(N^2)。
在已經(jīng)知道S[i..j]字母個數(shù)的情況下,我們可以直接推導(dǎo)出S[i..j+1]的不同字母個數(shù)。
首先我們需要改進
isLucky函數(shù):alphabet[] = false count = 0 isLucky(c):If not alphabet[c] Thenalphabet[c] = truecount = count + 1End IfReturn (count is Fibonaccid number)這里我們把
alphabet和count從函數(shù)中抽取出來,作為了全局變量。同時,isLucky的參數(shù)變?yōu)閱蝹€字符,每次將新增的S[j+1]加入統(tǒng)計中。下面是函數(shù)的主體部分:
For i = 0 .. len - 1alphabet[] = falsecount = 0For j = i .. len - 1// 此時isLucky返回的是S[i..j]的不同字母數(shù)量是否滿足條件If isLucky(S[j]) ThenansList.push(S[i..j])End IfEnd For End For最后只需要將
ansList所保存的子串進行排序去重后輸出,即可順利通過該題目。1 #include <iostream> 2 #include <string> 3 #include <cstring> 4 #include <cstdlib> 5 #include <set> 6 using namespace std; 7 string s; 8 9 bool IsFibonaccidNum(int n){ 10 return (n == 1 || n == 2 || n == 3 || n == 5 || n == 8 || n == 13 || n == 21); 11 } 12 13 bool isLucky(int i, int j){ 14 int alphabet[26] = {0}; 15 //memset(alphabet, 0, sizeof(alphabet)); 16 int count = 0; 17 for(int k = i; k <= j; k++){ 18 if(alphabet[s[k] - 'a'] == 0){ 19 alphabet[s[k] - 'a'] = 1; 20 count++; 21 } 22 } 23 return (IsFibonaccidNum(count)); 24 } 25 26 int main(){ 27 set<string> set1; 28 cin >> s; 29 int len = s.length(); 30 for(int i = 0; i < len; i++){ 31 for(int j = i; j < len; j++){ 32 if(isLucky(i, j)){ 33 string str = s.substr(i, j - i + 1); 34 set1.insert(str); 35 } 36 } 37 } 38 39 for(set<string>::iterator it = set1.begin(); it != set1.end(); it++){ 40 cout << *it << endl; 41 } 42 //system("pause"); 43 return 0; 44 }
1 #include <iostream> 2 #include <string> 3 #include <cstring> 4 #include <cstdlib> 5 #include <set> 6 using namespace std; 7 int alphabet[26] = {0}, cnt; 8 9 bool IsFibonaccidNum(int n){ 10 return (n == 1 || n == 2 || n == 3 || n == 5 || n == 8 || n == 13 || n == 21); 11 } 12 13 bool isLucky(char c){ 14 if(alphabet[c - 'a'] == 0){ 15 alphabet[c - 'a'] = 1; 16 cnt++; 17 } 18 return (IsFibonaccidNum(cnt)); 19 } 20 21 int main(){ 22 set<string> set1; 23 string s; 24 cin >> s; 25 int len = s.length(); 26 for(int i = 0; i < len; i++){ 27 memset(alphabet, 0, sizeof(alphabet)); 28 cnt = 0; 29 for(int j = i; j < len; j++){ 30 if(isLucky(s[j])){ 31 string str = s.substr(i, j - i + 1); 32 set1.insert(str); 33 } 34 } 35 } 36 37 for(set<string>::iterator it = set1.begin(); it != set1.end(); it++){ 38 cout << *it << endl; 39 } 40 //system("pause"); 41 return 0; 42 }
?
轉(zhuǎn)載于:https://www.cnblogs.com/qinduanyinghua/p/5828450.html
總結(jié)
以上是生活随笔為你收集整理的hihocoder 1152 Lucky Substrings的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 宫腔镜手术多少钱啊?
- 下一篇: 求蓝色天空歌词。