DNA Sequencing

Problem description
Finally, Plankton’s attempts to steal the Krabby Patty formula succeeded and it eventually put the Krusty Krab out of business. So, SpongeBob and his co-workers decided to switch to a brand new job. Their new startup is Krusty-Royan, a biological research institute whose main focus is on DNA sequencing. Their first customer is Sandy, the squirrel scientist, who has found the corpse of an alien from the outer space and asked Krusty-Royan crew to extract its DNA sequence. Contrary to the life on earth, the DNA of the alien was not only composed of the 4 well-known nucleotides (A, C, G, and T), but all 26 English letters! So, each part of its DNA is a sequence of capital English letters. Given the alien tissue, the DNA sequencer machine extracted a number of (not necessarily distinct) DNA sequences and printed them on paper, one per line. Based on the contract, a DNA sequence is valid only if its length is at least M, and Sandy will pay one dollar for each distinct valid DNA sequence. So, Mr. Krabs, the greedy boss of Krusty-Royan has asked SpongeBob to use a correction pen and erase some letters from the end of the sequences printed on the paper in order to maximize the number of distinct valid DNA sequences. Your job is to help SpongeBob find the maximum number of distinct valid DNA sequences he can make.
Input
There are multiple test cases in the input. Each test case starts with a line containing two space-separated integers k and M (1 ⩽ k ⩽ 500; 1 ⩽ M ⩽ 500). Each of the next k lines starts with a number ni followed by a string si which means there are ni copies of DNA sequence si printed on the paper (1 ⩽ ni ⩽ 500). The length of the strings is a positive integer not greater than 500. The input terminates with a line containing 0 0 which should not be processed as a test case.
Output
For each test case, output a line containing the maximum number of distinct valid DNA sequences which SpongeBob can provide.
Sample Input
2 1
2 ABB
2 ABC
2 2
2 ABB
2 ABC
2 3
2 ABB
2 ABC
2 4
2 ABB
2 ABC
0 0
Sample Output
4
3
2
0
Problem Source
Asia Region, Tehran Site,Sharif University of Technology, 24?25 December 2015

题解:建立一棵字典树,dfs()判断满足条件的子串。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=505;
typedef long long ll;
int n,k,num;
char s[maxn];
struct Trie{
      int tot,root,ch[maxn*maxn][30],h[maxn*maxn],cnt[maxn*maxn];
      int newnode(int d){
         h[tot]=d;cnt[tot]=0;
         return tot++;
      }
      void init(){
         tot=0;
         root=newnode(0);
         memset(ch,-1,sizeof(ch));
         memset(cnt,0,sizeof(cnt));
      }
      void ins(char s[],int len,int c){
         int now=root,nx;
         for(int i=0;i<len;i++){
            int tmp=s[i]-'A';
            if(ch[now][tmp]==-1) ch[now][tmp]=newnode(i+1);
            now=ch[now][tmp];
         }
         cnt[now]=c;
      }
      int dfs(int cur){
          int ret=0;
          for(int i=0;i<26;i++)
            if(ch[cur][i]!=-1) ret+=dfs(ch[cur][i]),cnt[cur]+=max(cnt[ch[cur][i]]-1,0);
          if(cnt[cur]&&h[cur]>=k) ret++;
          return ret;
      }

}trie;
int main(){
     while(scanf("%d%d",&n,&k)&&n+k){
        trie.init();
        for(int i=0;i<n;i++) scanf("%d%s",&num,s),trie.ins(s,strlen(s),num);
        printf("%d\n",trie.dfs(0));
     }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值