POJ 2945 Find the Clones
题目描述
Doubleville, a small town in Texas, was attacked by the aliens. They have abducted some of the residents and taken them to the a spaceship orbiting around earth. After some (quite unpleasant) human experiments, the aliens cloned the victims, and released multiple copies of them back in Doubleville. So now it might happen that there are 6 identical person named Hugh F. Bumblebee: the original person and its 5 copies. The Federal Bureau of Unauthorized Cloning (FBUC) charged you with the task of determining how many copies were made from each person. To help you in your task, FBUC have collected a DNA sample from each person. All copies of the same person have the same DNA sequence, and different people have different sequences (we know that there are no identical twins in the town, this is not an issue).
根据以上描述信息可以将本题抽象为:给出一给字符串,数量为n,每个字符串的长度为m,输出出现i(1<=i<=n)次的字符串的个数。
解题思路:
最近在学习字典树,刚开始看到这题也想用字典树来做。思考后发现其实可以用更简单的map来实现。定义类型为map<char*, int>的变量,记录字符串出现的次数。然后开辟数组num[20000],记录出现i(1<=i<=n)次的字符串的个数。再遍历map,以当前字符串出现的字数减1记为k作为索引,更新num[k],使得num[k]++。
实现时需要注意的地方:
1、因为在定义map时的key类型是char *,因此需要自定义比较函数。
2、为避免每次都new新的字符串,定义字符数组,需要new的地方,直接从数组中取。
Memory: 1184K Time: 2938MS
#include <cstdio>
#include <map>
#include <cstring>
using namespace std;struct cmp //自定义比较函数
{bool operator()(const char *s1, const char *s2) const{return strcmp(s1, s2) < 0;}
};int main()
{char *dna;char dnas[20000][21]; //字符数组,需要new的地方直接取int num[20000];map<char*, int, cmp> mp;int i, j, m, n;while(scanf("%d%d", &n, &m)){if(n == 0 && m == 0)break;j = 0;mp.clear();for(i = 0; i < n; i++){dna = dnas[j++]; //从数组中直接取scanf("%s", dna);mp[dna]++;}memset(num, 0, sizeof(num));map<char*, int, cmp>::iterator iter = mp.begin();while(iter != mp.end()){num[iter->second - 1]++;iter++;}for(i = 0; i < n; i++){printf("%d\n", num[i]);}}return 0;
}
利用map实现,程序的执行效率比较低。有时间再用Trie方法实现,比较两者的效率(猜测Trie肯定比map实现高效)。
POJ 2945 Find the Clones相关推荐
- poj 2945 Find the Clones 用trie树实现
/*原来动态构树是这么费时间的...20000个单词最长为20个的题,跑到了1688MS,题目: 题目大概是说找出相同的字符串并且统计个数.分析: 因为昨天刚学完trie树,现在练了几道,有点感觉了, ...
- hdu与poj题目分类
POJ 初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. (5)构造法.(po ...
- POJ 3268 D-Silver Cow Party
http://poj.org/problem?id=3268 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...
- poj 2559 Largest Rectangle in a Histogram 栈
// poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...
- POJ 2528 Mayor's posters(线段树)
题目大意 贴海报.每张海报的高度都是一样的,唯独宽度不一样.每张海报只能占用整数倍的单位线段长度,贴了 n(n<=10000) 张海报之后,有几张能够看见(有一个角能看见这张海报也算被看见了)? ...
- POJ 2226 Muddy Fields 最小点覆盖+加建图(好题)
题目链接 题目一看就是最小点覆盖,这道题与POJ - 3041 算是一类题,但是3041算是一道十分裸的,因为删除的是整行或者整列,所以图其实是现成的,但是本题的难点就在如何建图. 思路:首先还是尽量 ...
- poj 2681 字符串
http://poj.org/problem?id=2681 给你任意长度的字符串,找出两串字符中不相同的字符个数(总数) #include<string> #include<cst ...
- (区间dp 或 记忆化搜素 )Brackets -- POJ -- 2955
http://poj.org/problem?id=2955 Description We give the following inductive definition of a "reg ...
- POJ 2586 Y2K Accounting Bug(贪心)
题目连接:http://poj.org/problem?id=2586 题意:某公司要统计全年盈利状况,对于每一个月来说,如果盈利则盈利S,如果亏空则亏空D.公司每五个月进行一次统计,全年共统计8次( ...
最新文章
- [更新问题]无法在安装新的版本前,为“./boot/vmlinuz-2.6.24-19-generic”做一个符号链接备份...
- VScode Python插件
- android 手机关机代码非root,怎么用代码判断android手机是否开启了ROOT 权限知道...
- android studio升级失败提示 Connection failed解决方法
- 个人收集 - 1、自动消失的消息提示(Js+Div实现)
- 论文浅尝 - AAAI2020 | 多轮对话系统中的历史自适应知识融合机制
- 小白零基础学习Java开发入门教程奉上,希望对你有所帮助!
- dos 改某个目录下所有文件的时间_go语言入门学习笔记(2)-DOS操作及go语言变量学习...
- 转: MySql数据引擎简介与选择方法
- 编译安装mysql5.7.24踩的坑
- 【OpenCV】边缘检测
- css设置按钮竖直方向居中_button内flex垂直居中竟然不居中的解决
- 2019最新版QQ音乐api调用(原创)
- 计算机专业毕业论文题目大全集
- 马氏距离(Mahalanobis Distance)
- 【jQuery】jQuery对象与prevObject
- 华为手机word插件加载失败_word加载项启动失败
- 编写一个触发器:当修改一个学生专业的时候(转专业),则自动将本学期新专业同学全都选修的课程给该同学选修
- 高等数学(第七版)同济大学 习题6-2 (后18题)个人解答
- Redis并发竞争key的解决方案详解