这是上届师兄布置的字典树题目,上学期到现在拖了很久都没看出其中问题的本质,这个假期准备学习ac自动机,就又把字典树过了一遍,又遇到了这题,想到逃避不是办法。我真菜,起初一直往树的构成去想,后面观察了几遍数据,发现只要数据不重复,则只需一把扫把,主要是整数过大,只能用字符串存,就用字典树存起来,在串的尾部++flag[root],比较取出最大的flag,则是答案,如0004,4,04这些数据要处理为4。了解了题意,用hash也能过吧。
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1e5 + 5;
int tot, trie[N][11], ans;
int flag[N];
void init(){
for(int i = 0; i <= tot; ++i){
flag[i] = 0;
for(int j = 0; j < 11; ++j){
trie[i][j] = 0;
}
}
tot = 0;
}
void insert(char *str)
{
int len = strlen(str);
int root = 0;
for(int i = 0; i < len; ++i)
{
int id = str[i] - '0';
if(!trie[root][id]) trie[root][id] = ++tot;
root = trie[root][id];
}
++flag[root];
if(flag[root] > ans){
ans = flag[root];
}
}
int main()
{
int n;
tot = 0;
while(scanf("%d", &n) != EOF){
ans = 0;
for(int i = 0; i < n; ++i){
char tmp[35], s[35];
scanf("%s", tmp);
int h = 0, flag = 0;
for(int i = 0; i < strlen(tmp); ++i){
if(tmp[i] == '0' && flag == 0){
continue;
}
flag = 1;
s[h] = tmp[i];
++h;
}
s[h] = '\0';
insert(s);
}
printf("%d\n", ans);
init();
}
return 0;
}
能学多少是多少了,呆在那个地方我只是想巩固好数据结构,到那个时候可以少复习一门,拿奖什么的就随缘吧。