POJ 3080 Blue Jeans Trie suffix tree Solution

Source: Internet
Author: User

POJ 3080 Blue Jeans Trie suffix tree Solution

The question is what Jeans mean, but I don't know what the question is about with Blue Jeans.

The data in this question is very watery and the data volume is small. Therefore, we can use very violent methods or KMP, which is less violent.

Here we use a more violent Trie suffix tree. This solution is useless.

Ideas:

1. Create the suffix Trie tree for all strings

2. Add additional information to check whether all strings have passed through the path. If yes, it is a valid string. Search for the longest string like this.

3. Optimization: if not all the path passing through the string, you can directly return the result without searching.

Finally, I found that deleting Trie is time-consuming, so the speed is much faster without releasing Trie, and the memory is naturally much higher.


# Include
 
  
# Include
  
   
Const int MAX_N = 61; const int ALP_LEN = 4; const int SIGNIFICANT = 3; char dna str [MAX_N]; inline int charToint (char a) {switch () {case 'A': return 0; case 'T': return 1; case 'G': return 2; case 'C': return 3;} return-1; // unexpected input} inline char intTochar (int I) {switch (I) {case 0: return 'a'; case 1: return 'T'; case 2: return 'G'; case 3: return 'C';} return '? '; // Unexpected input} struct Node {int n, tab; Node * alpha [ALP_LEN]; Node (): n (0), tab (-1) {for (int I = 0; I <ALP_LEN; I ++) {alpha [I] = NULL ;}}; void delTrie (Node * rt) {if (rt) {for (int I = 0; I <ALP_LEN; I ++) {if (rt-> alpha [I]) delTrie (rt-> alpha [I]); rt-> alpha [I] = NULL;} delete rt;} Node * Trie; int tab; void insert (char * chs, int len) {Node * pCrawl = Trie; for (int I = 0; I <len; I ++) {int k = charToint (chs [I]); if (! PCrawl-> alpha [k]) pCrawl-> alpha [k] = new Node; pCrawl = pCrawl-> alpha [k]; if (pCrawl-> tab! = Tab) // prevent Repeated Computation {pCrawl-> tab = tab; pCrawl-> n ++ ;}} if (pCrawl-> tab! = Tab) {pCrawl-> tab = tab; pCrawl-> n ++;} int maxLen, pid, n; char path [MAX_N], res [MAX_N]; void search (Node * rt) {for (int I = 0; I <ALP_LEN; I ++) {if (rt-> alpha [I] & rt-> alpha [I]-> n = n) {path [pid ++] = intTochar (I ); if (maxLen <pid) {maxLen = pid; path [pid] = '\ 0'; strncpy (res, path, pid + 1 );} search (rt-> alpha [I]); pid -- ;}} int main () {int T; scanf ("% d", & T ); while (T --) {Trie = new Node; scanf ("% d", & n); getchar (); // get rid of '\ n' tab = 0; for (int I = 0; I <n; I ++) {gets (DNA Str); int len = MAX_N-1; tab ++; for (char * p = & dna str [0]; * p; p ++) {insert (p, len); len -- ;}} maxLen = 0, pid = 0; search (Trie); if (maxLen <SIGNIFICANT) {puts ("no significant commonalities");} else {printf ("% s \ n", res );} // delTrie (Trie);} return 0 ;}
  
 



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.