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
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Raoxiaomi.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值