[UVA][Trie] 760 - DNA Sequencing@Morris' Blog|PChome Online 人新台
2013-02-26 08:26:42| 人1,442| 回0 | 上一篇 | 下一篇

[UVA][Trie] 760 - DNA Sequencing

0 收藏 0 0 站台


  DNA Sequencing 

A DNA molecule consists of two strands that wrap around each other to resemble a twisted ladder whose sides, made of sugar and phosphate molecules, are connected by rungs of nitrogen-containing chemicals called bases. Each strand is a linear arrangement of repeating similar units called nucleotides, which are each composed of one sugar, one phosphate, and a nitrogenous base. Four different bases are present in DNA: adenine (A), thymine (T), cytosine (C), and guanine (G). The particular order of the bases arranged along the sugar-phosphate backbone is called the DNA sequence; the sequence specifies the exact genetic instructions required to create a particular organism with its own unique traits.


Geneticists often compare DNA strands and are interested in finding the longest common base sequence in the two strands. Note that these strands can be represented as strings consisting of the letters a, t, c and g. So, the longest common sequence in the two strands atgc and tga is tg. It is entirely possible that two different common sequences exist that are the same length and are the longest possible common sequences. For example in the strands atgc and gctg, the longest common sequences are gc and tg.

Input and Output 

Write a program that accepts as input two strings representing DNA strands, and prints as output the longest common sequence(s) in lexicographical order.

If there isn't any common sequence between the two strings, just print: ``No common sequence."

If there are more than one test cases, it must be a blank line between two consecutive, both in input and output files.

The strings are at most 300 characters-long.

Sample Input 

atgc tga atgc gctg 

Sample Output 

tg gc tg


原本要用 SA 去做的,但很小,length < 300。
用 O(n*n) 建,一用 O(n*n) 去 match 棵。
然後出即可。

很妙的是,我一始就 RE 了,但是 UVa 上是 WA,我一直抓不到 BUG。

#include <stdio.h>
#include <string.h>
struct TrieNd{
int LK[26];
int ac;
} ND[100000];
int TrieSize = 0;
void insTrie(char s[]) {
static int i, idx, v;
for(i = 0, idx = 0; s[i]; i++) {
v = s[i]-'a';
if(!ND[idx].LK[v]) {
TrieSize++;
memset(&ND[TrieSize], 0, sizeof(ND[0]));
ND[idx].LK[v] = TrieSize;
}
idx = ND[idx].LK[v];
}
}
void matchTrie(char s[]) {
static int i, idx, v;
for(i = 0, idx = 0; s[i]; i++) {
v = s[i]-'a';
ND[idx].ac = 1;
if(ND[idx].LK[v])
idx = ND[idx].LK[v];
else
break;
}
ND[idx].ac = 1;
}
int mxlen = -1;
void dfs(int nd, int dep) {
int i;
for(i = 0; i < 26; i++) {
if(ND[nd].LK[i] && ND[ND[nd].LK[i]].ac) {
if(dep+1 > mxlen)
mxlen = dep+1;
dfs(ND[nd].LK[i], dep+1);
}
}
}
char path[512];
void dfs2(int nd, int dep) {
int i;
for(i = 0; i < 26; i++) {
if(ND[nd].LK[i] && ND[ND[nd].LK[i]].ac) {
path[dep] = i+'a';
if(dep+1 == mxlen) {
path[dep+1] = '\0';
puts(path);
}
dfs2(ND[nd].LK[i], dep+1);
}
}
}
int main() {
char s1[512], s2[512];
int i, first = 0;
while(gets(s1)){
gets(s2);
if(first) puts("");
first = 1;
memset(&ND[0], 0, sizeof(ND[0]));
TrieSize = 0;
for(i = 0; s1[i]; i++)
insTrie(s1+i);
for(i = 0; s2[i]; i++)
matchTrie(s2+i);
mxlen = -1;
dfs(0, 0);
if(mxlen > 0)
dfs2(0, 0);
else
puts("No common sequence.");
gets(s1);
}
return 0;
}

台: Morris
人(1,442) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][math] 11480 - Jimmy's Balls
此分上一篇:[UVA][多形] 11473 - Campus Roads

<-- loop end -->
是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86