trie字典树解决问题

24 篇文章 0 订阅

问题:
Xuemei’s question
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256.000MB,其他语言512.000MB
64bit IO Format: %lld
题目描述
Several weeks ago, a beautiful xuemei from College of Biology ask LYX a bioinformatics question, k-mer frequency count.

What is k-mer?

k-mer is a DNA sequence of length k:

ATTG is a 4-mer.

ATGGAC is a 6-mer.

For example, a sequence of length 16 can extract 11 k-mers of length 6.

AGGATGAGACAGATAG

AGGATG

GGATGA

GATGAG

ATGAGA

TGAGAC

 GAGACA

  AGACAG

   GACAGA

    ACAGAT

     CAGATA

      AGATAG

Xuemei ask LYX to count the frequency of all k-mers of a DNA of length n.

LYX is very smart, he finish it quickly, and win her heart. Now imagine that there are many beautiful girls ask you to count the k-mer frequency, in order to win their hearts, you must finish this task as fast as possible.

输入描述:
Input include 2
lines, the first is n(1<=n<=100) and k(1<=k<=n), separated by a
blank, the second is a DNA sequence.

Output descript
输出描述:
Output m lines, m
is total number of all the different k-mer, each line output the k-mer sequence
and the frequency of the k-mer(keep six decrimal places), separated by a blank.
The k-mer sorted by dictionary order.
样例1
输入
6 3
ATCGTT
输出
ATC 0.250000
CGT 0.250000
GTT 0.250000
TCG 0.250000
说明
none
样例2
输入
4 1
ATCA
输出
A 0.500000
C 0.250000
T 0.250000
说明
none
样例3
输入
9 7
GTGAATTTC
输出
GAATTTC 0.333333
GTGAATT 0.333333
TGAATTT 0.333333
说明
none
例1
例2
1
C++11(clang++ 3.9)重置自测
1
#include
2
#include
3

4
#include
5
using namespace std;
6
char ans[100];
7
int turnI(char c)
8
{
9
if(c==‘A’)return 0;
10
if(c==‘C’)return 1;
11
if(c==‘G’)return 2;
12
if(c==‘T’)return 3;
13
}
14
char turnC(int c){
15
if(c0)return ‘A’;
16
if(c
1)return ‘C’;
17
if(c2)return ‘G’;
18
if(c
3)return ‘T’;
19
}
20
struct tree{
21
tree *next[26];
22
double times;
23
tree(){
24
for(int i=0;i<4;++i)next[i]=NULL;
25
times=0;
26
}
27
};
28
void createTree(string s,int be,int en,tree *cur){
29
cur->times=cur->times+1;
30
if(be==en)return ;
31

32
if(cur->next[turnI(s[be])]){
33
// cout<<‘0’;
34
// cout<<s[be]<<endl;
35
createTree(s,be+1,en,cur->next[turnI(s[be])]);
36

保存并调试
代码:

#include<iostream>
#include<string>

#include <iomanip>
using namespace std;
    char ans[100];
int turnI(char c)
{
    if(c=='A')return 0;
    if(c=='C')return 1;
    if(c=='G')return 2;
    if(c=='T')return 3;
}
char turnC(int c){
    if(c==0)return 'A';
    if(c==1)return 'C';
    if(c==2)return 'G';
    if(c==3)return 'T';
}//转换函数 
struct tree{
    tree *next[26];
    double times;
    tree(){
        for(int i=0;i<4;++i)next[i]=NULL;
        times=0;
    }
};//节点及初始化 
void createTree(string s,int be,int en,tree *cur){
	cur->times=cur->times+1;
    if(be==en)return ;
    
    if(cur->next[turnI(s[be])]){//在存在节点建树 
    //	cout<<'0';
    //	cout<<s[be]<<endl;
        createTree(s,be+1,en,cur->next[turnI(s[be])]);

    }
    else {
    //	cout<<'1';
    //	cout<<s[be]<<endl;
        cur->next[turnI(s[be])]=new tree();
        createTree(s,be+1,en,cur->next[turnI(s[be])]);//在不存在节点建树 
         }
}
void printTreeC(tree *cur,int curr,int b,double all)
{//打印函数 
	if(curr==b)
	{
		for(int i=0;i<b;++i)
		cout<<ans[i];
		cout << fixed <<setprecision(6)<<' '<<cur->times/all<<endl;
		return ;
	}
	if(cur->next[0]){
    	ans[curr]=turnC(0);
    	printTreeC(cur->next[0],curr+1,b,all);
	}
	if(cur->next[1]){
    	ans[curr]=turnC(1);
    	printTreeC(cur->next[1],curr+1,b,all);
	}
    if(cur->next[2]){
    	ans[curr]=turnC(2);
    	printTreeC(cur->next[2],curr+1,b,all);
	}
    if(cur->next[3]){
    	ans[curr]=turnC(3);
    	printTreeC(cur->next[3],curr+1,b,all);
	}
}
void printTree(tree *cur,int b){

    double all=cur->times;
    if(cur->next[0]){
    	ans[0]=turnC(0);
    	printTreeC(cur->next[0],1,b,all);
	}
	if(cur->next[1]){
    	ans[0]=turnC(1);
    	printTreeC(cur->next[1],1,b,all);
	}
    if(cur->next[2]){
    	ans[0]=turnC(2);
    	printTreeC(cur->next[2],1,b,all);
	}
    if(cur->next[3]){
    	ans[0]=turnC(3);
    	printTreeC(cur->next[3],1,b,all);
	}
}

int main()
{
    int a,b;
    tree *root=new tree();
    string s;
    cin>>a>>b;
    cin>>s;
 //   cout<<s;
    for(int i=0;i<=a-b;++i)
    {
        createTree(s,i,i+b,root);
    }
	printTree(root,b);
}
/*
6 3
ATCGTT
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值