首页 > 其他 > 详细

light oj 1224 (字典树)

时间:2020-07-27 09:35:30      阅读:77      评论:0      收藏:0      [点我收藏+]

本题题意是让你找出一个子字符串,使该字符串作为所有插入字符串前缀的次数 * 该字符串的长度 结果最大
所以我们可以在插入字符串的时候进行统计,用一个maxx和sum[root] ? 当前长度len 比较取大即可

#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;
const int maxn = 2e6 + 10;
int trie[maxn][4];
int sum[maxn];
int maxx = 0;
int tot = 0;

void insert(char *s){
	int len = strlen(s);
	int rt = 0;
	for(int i = 0; i < len; ++ i){
		int id = 0;
		if(s[i] == ‘C‘)
			id = 1;
		else if(s[i] == ‘G‘)
			id = 2;
		else if(s[i] == ‘T‘)
			id = 3;
		if(trie[rt][id] == 0)
			trie[rt][id] = ++ tot;
		rt = trie[rt][id];
		sum[rt] ++;
		maxx = max(maxx, sum[rt] * (i + 1));
	}
}

int n, t;
char s[52];
int main(){
	int cnt = 1;
	for(scanf("%d", &t); t; --t){
		scanf("%d", &n);
		tot = 0;
		maxx = 0;
		for(int i = 0; i < n; ++ i){
			scanf("%s", s);
			insert(s);
		}
		printf("Case %d: %d\n", cnt++, maxx);
		for(int i = 0; i <= tot; ++ i){
			sum[i] = 0;
			for(int j = 0; j < 4; ++ j)
				trie[i][j] = 0;
		}
	}
}

light oj 1224 (字典树)

原文:https://www.cnblogs.com/wanshe-li/p/13382816.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!