SPOJ Find theClones 字典树/Map 两种方法都有

题目

Find the Clones
Vjudge传送门
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).

Input
The input contains several blocks of test cases. Each case begins with a line containing two integers: the number 1 <= n <= 20000 people, and the length 1 <= m <= 20 of the DNA sequences. The next n lines contain the DNA sequences: each line contains a sequence of m characters, where each character is either ‘A’, ‘C’, ‘G’ or ‘T’.
The input is terminated by a block with n = m = 0 .

Output
For each test case, you have to output n lines, each line containing a single integer. The first line contains the number of different people that were not copied. The second line contains the number of people that were copied only once (i.e., there are two identical copies for each such person.) The third line contains the number of people that are present in three identical copies, and so on: the i -th line contains the number of persons that are present in i identical copies. For example, if there are 11 samples, one of them is from John Smith, and all the others are from copies of Joe Foobar, then you have to print ‘1’ in the first andthe tenth lines, and ‘0’ in all the other lines.

Example(样例)
Input:

9 6
AAAAAA
ACACAC
GTTTTG
ACACAC
GTTTTG
ACACAC
ACACAC
TCCCCC
TCCCCC
0 0

Output:

1
2
0
1
0
0
0
0
0

题目大意

找有多少组相同的串,第几行输出有几串字符串是相同的。

解题思路

好像可以用map写,可以用Hash写,但是我还是用了字典树去写
这道题还好,多组输入的题。记住要重置trie树,否则会WA,还有end数组,用来记录的也要在每一次搜索后清空,否则会重复记录这个副本的数量。
数据要开到1e6,不然会RE。
后面发现map好像用的时间空间更少,就用了map再过了一遍。

代码

字典树

#include<bits/stdc++.h>
#define IOS std::ios::sync_with_stdio(false)
int tree[1000005][25], tot = 0, end[1000005], ans[1000005];
char book[4] = {'A', 'T', 'C', 'G'}; //查找下一个
void insert(std::string str) {
	int pos = 0;
	for (int i = 0; i < str.length(); i++) {
		int c = 0;
		for(int j = 0; j < 4; j++) if(book[j] == str[i]){c = j; break;}
		if (tree[pos][c] == 0) tree[pos][c] = ++tot;
		pos = tree[pos][c];
	}
	end[pos]++;
}
void query(std::string str) {
	int pos = 0;
	for (int i = 0; i < str.length(); i++) {
		int c = 0;
		for(int j = 0; j < 4; j++) if(book[j] == str[i]) {c = j; break;}
		if (tree[pos][c] == 0) return;
		pos = tree[pos][c];
	}
	if(end[pos]) ans[end[pos]]++;
	end[pos] = 0;
}
std::vector<std::string> t; //用来存每一次读入的数据
void init(int n){
	for(int i = 0; i <= n; i++) ans[i] = 0;
}
int main(int argc, char** argv) {
	IOS; //加速
	int n, m;
	while(std::cin >> n >> m){
		if(!n && !m) break;
		tot = 0;	//每次都要从零开始不然就会RE
		memset(tree, 0, sizeof(tree));
		t.clear();
		ans[0] = n;
		for(int i = 0; i < n; i++){
			std::string str;
			std::cin >> str;
			t.push_back(str);
			insert(str);
		}
		for(int i = 0; i < n; i++) query(t[i]);
		for(int i = 1; i <= n; i++){
			std::cout << ans[i] << std::endl;
		}
		init(n);
	}
	return 0;
}

Map

#include<bits/stdc++.h>
#define IOS std::ios::sync_with_stdio(false)
int ans[1000005];
std::map<std::string, int> ap;
void init(int n){
	for(int i = 0; i <= n; i++) ans[i] = 0;
}
int main(int argc, char** argv) {
	IOS;
	int n, m;
	while(std::cin >> n >> m){
		ap.clear();
		for(int i = 0; i < n; i++){
			std::string str; std::cin >> str;
			ap[str]++;
		}
		std::map<std::string, int>::iterator it;
		for(it = ap.begin(); it != ap.end(); it++){
			ans[it->second]++;
		}
		for(int i = 1; i <= n; i++) std::cout << ans[i] << std::endl;
		init(n);
	}
	return 0;
}

混合法

#include<bits/stdc++.h>
int tree[500005][5], tot = 0, end[500005], ans[500005];
char book[4] = {'A', 'T', 'C', 'G'};
void insert(char* str) {
	int pos = 0, len = strlen(str);
	for (int i = 0; i < len; i++) {
		int c = 0;
		for(int j = 0; j < 4; j++) if(book[j] == str[i]){c = j; break;}
		if (tree[pos][c] == 0) tree[pos][c] = ++tot;
		pos = tree[pos][c];
	}
	end[pos]++;
}
void query(std::string s) {
	int pos = 0, len = s.length();
	for (int i = 0; i < len; i++) {
		int c = 0;
		for(int j = 0; j < 4; j++) if(book[j] == s[i]) {c = j; break;}
		if (tree[pos][c] == 0) return;
		pos = tree[pos][c];
	}
	if(end[pos]) ans[end[pos]]++;
	end[pos] = 0;
}
std::map<std::string, int>ap;
char str[20];
void init(int n){
	for(int i = 0; i <= n; i++) ans[i] = 0;
}
int main(int argc, char** argv) {
	int n, m;
	while(scanf("%d%d", &n, &m) && n && m){
		getchar();
		tot = 0;
		memset(tree, 0, sizeof(tree));
		ap.clear();
		for(int i = 0; i < n; i++){
			scanf("%s", str);
			std::string temp = str;
			ap[temp] = 1;
			insert(str);
		}
		for(std::map<std::string, int>::iterator it = ap.begin(); it != ap.end(); it++) query(it->first);
		for(int i = 1; i <= n; i++){
			printf("%d\n", ans[i]);
			//std::cout << ans[i] << std::endl;
		}
		init(n);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Raoxiaomi.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值