http://poj.org/problem?id=3294
Life Forms Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 9931 Accepted: 2739Description
You may have wondered why most extraterrestrial life forms resemble humans, differing by superficial traits such as height, colour, wrinkles, ears, eyebrows and the like. A few bear no human resemblance; these typically have geometric or amorphous shapes like cubes, oil slicks or clouds of dust.
The answer is given in the 146th episode of Star Trek - The Next Generation, titled The Chase. It turns out that in the vast majority of the quadrant's life forms ended up with a large fragment of common DNA.
Given the DNA sequences of several life forms represented as strings of letters, you are to find the longest substring that is shared by more than half of them.
Input
Standard input contains several test cases. Each test case begins with 1 ≤ n ≤ 100, the number of life forms. n lines follow; each contains a string of lower case letters representing the DNA sequence of a life form. Each DNA sequence contains at least one and not more than 1000 letters. A line containing 0 follows the last test case.
Output
For each test case, output the longest string or strings shared by more than half of the life forms. If there are many, output all of them in alphabetical order. If there is no solution with at least one letter, output "?". Leave an empty line between test cases.
Sample Input
3 abcdefg bcdefgh cdefghi 3 xxx yyy zzz 0
Sample Output
bcdefg cdefgh ?
Source
Waterloo Local Contest, 2006.9.30題意:求一個長度最大的字符串,使其在超過一半的給定字符串中出現,存在多個按字典序輸出。
思路:用不同的分隔符把給定字符串拼起來。然後我們二分答案,掃描height數組,判斷長度為p的串是否連續超過一半串中連續出現,注意這裡並不是連續n/2個連續height數組裡的,而是屬於不同給定字符串的n/2個,所以可以開始預處理每一個後綴屬於哪個字符串。最後輸出和判斷一樣,按照字典序由於是掃描height數組所以保證了。
RE了好多發,發現是這個問題,在分隔字符串時,s[n]='z'+i,a[n]=s[n]-'a'+i+1;這樣寫會出問題233,a[n]賦'z'-'a'+1+i就好了。
poj用set判斷會超時。。
/** * @author neko01 */ //#pragma comment(linker, "/STACK:102400000,102400000") #include#include #include #include #include #include #include #include #include #include