light oj 1224 (字典树)

本题题意是让你找出一个子字符串,使该字符串作为所有插入字符串前缀的次数 * 该字符串的长度 结果最大
所以我们可以在插入字符串的时候进行统计,用一个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;
		}
	}
}

猜你喜欢

转载自www.cnblogs.com/wanshe-li/p/13382816.html