https://vjudge.net/problem/POJ-2945

题意:

给出n个长度相同的DNA序列,如果一个DNA序列出现过两次,那么就有说明它被复制了一次。问被复制0次,1次,2次……n-1次的DNA序列分别有多少个。

思路:

可以利用字典树的方法做,用map目测会超时。因为一个字符串是它自己本身的假前缀,所以把每一个字符串的前缀数量-1,就是它被复制的数目。这题学到了一个技巧,统计的时候,已经统计过的要跳开,一开始用map判断超时了,之后想到用sort对char类型的字符串进行排序,但是sort是不能对char的字符串进行排序。但是,我们有结构体啊,把字符串封装在一个结构体中,自己写一个cmp函数就完美的解决了这个问题。这题还是字典树模板用上啦。一定记住最后要释放内存啊。QAQ

代码:

 #include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int a[]; const int maxn = ; struct str
{
char s[];
}b[]; bool cmp(str aa,str bb)
{
return strcmp(aa.s,bb.s) < ;
} struct trie
{
trie *next[maxn]; int flag; trie()
{
flag = ;
memset(next,NULL,sizeof(next));
}
}*root; void insert(char *str)
{
int len = strlen(str);
trie *p = root,*q; for (int i = ;i < len;i++)
{
int id = str[i] - 'A'; if (p -> next[id] == NULL)
{
q = new trie();
p -> next[id] = q;
p = p -> next[id];
}
else
{
p = p->next[id]; ++(p->flag);
}
}
} int query(char *str)
{
int len = strlen(str);
trie *p = root; for (int i = ;i < len;i++)
{
int id = str[i] - 'A'; p = p -> next[id]; if (p == NULL) return ;
} return p->flag;
} void Free(trie* t)
{
if (t == NULL) return; for (int i = ;i < maxn;i++)
{
if (t -> next[i]) Free(t->next[i]);
} delete(t);
} int main()
{
int n,m; while (scanf("%d%d",&n,&m) != EOF)
{
if (m == && n == ) break; memset(a,,sizeof(a)); root = new trie(); for (int i = ;i < n;i++)
{
scanf("%s",b[i].s);
insert(b[i].s);
} sort(b,b+n,cmp); //for (int i = 0;i < n;i++)
//printf("%s\n",b[i].s); int rs = query(b[].s); char ss[];strcpy(ss,b[].s); a[rs-]++; for (int i = ;i < n;i++)
{
if (strcmp(ss,b[i].s) == ) continue; strcpy(ss,b[i].s); rs = query(b[i].s); a[rs-]++;
} for (int i = ;i < n;i++)
{
printf("%d\n",a[i]);
} Free(root);
} return ;
}

poj 2945 Find the Clones的更多相关文章

  1. POJ 2945 trie树

    Find the Clones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7704 Accepted: 2879 Descr ...

  2. poj 2945 trie树统计字符串出现次数

    用记录附加信息的val数组记录次数即可. trie的原理:每个可能出现的字目给一个编号c,那么整个树就是一个c叉树 ch[u][c]表示 节点u走c边过去之后的节点 PS:trie树还有种动态写法,使 ...

  3. 字典树trie的学习与练习题

    博客详解: http://www.cnblogs.com/huangxincheng/archive/2012/11/25/2788268.html http://eriol.iteye.com/bl ...

  4. Find the Clones(字典树)

    链接:http://poj.org/problem?id=2945 Description Doubleville, a small town in Texas, was attacked by th ...

  5. POJ2945:Find the Clones——题解

    http://poj.org/problem?id=2945 还是trie树……对于结束标记累加并且开个数组记录一下即可. #include<cstdio> #include<cst ...

  6. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  7. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  8. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  9. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

随机推荐

  1. JDBC控制事务

    概念 事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元(unit).事务通常由高级数据库操纵语言或编程语言(如SQL,C++或Java)书写的用户程序的执行所引起,并 ...

  2. Atlas安装配置

    准备环境 192.168.1.1(Altas) 192.168.1.2(MySQL主) 192.168.1.3(MySQL从) 官方链接:https://github.com/Qihoo360/Atl ...

  3. [模拟赛] T2 混合图

    Description Hzwer神犇最近又征服了一个国家,然后接下来却也遇见了一个难题. Hzwer的国家有n个点,m条边,而作为国王,他十分喜欢游览自己的国家.他一般 会从任意一个点出发,随便找边 ...

  4. Vue解析四之注册变量

    判断监听的变量,如果undefined可以用$set来注册一个变量. 另外click可以是表达式,不一定必须是一个方法.

  5. express+mongodb+socket.io

    node后端代码 // Setup basic express server var express = require('express'); var app = express(); var pa ...

  6. Dagger2的基本概念与实际应用。

    本文系原创博客,文中不妥烦请指出,如需转载摘要请注明出处! Dagger2的基本概念与实际应用 Alpha Dog 2016-11-30  10:00:00 本文Demo的github地址:https ...

  7. redis客户端可以连接集群,但JedisCluster连接redis集群一直报Could not get a resource from the pool

    一,问题描述: (如题目)通过jedis连接redis单机成功,使用JedisCluster连接redis集群一直报Could not get a resource from the pool 但是使 ...

  8. python socket编程制作后门木马(原创)

    不知道为啥,懒得解释怎么写出来的,直接上代码,爱看不看 Client:  简单连接服务器 #!/usr/bin/python #coding:utf-8 import socket import sy ...

  9. linux小白成长之路6————安装Java+Apache(httpd)+Tomcat

    [内容指引] 安装Java环境: 查看JDK版本: 安装Apache(httpd); 安装Tomcat: 设置服务开机启动. 1.安装Java环境 指令: yum intall java-1.8.0* ...

  10. Git中一些远程库操作的细节

    最近在公司,老是遇到Git远程操作的问题,现总结如下: 1,本地checkout一个新的分支,向远程push的时候,若远程没有该分支,会新建一个. 2.将远程代码clone到本地修改并commit后, ...