题意:士兵要学骑扫帚。每个士兵有一个level,level高的能在同一把扫帚上教level低的怎么骑。一个人最多有一个老师,一个学生。也可以没有。给n个士兵的level值,问最少需要多少扫帚。
mark:显然就是找出现次数最多的数字的次数。但因为数字长度多达30个字符,因此long long都存不下,用字符串。
wa了一次,忘记处理前导0。
AC代码:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
struct Trie
{
int n;
Trie *next[10];
Trie() //构造函数
{
n = 0;
memset(next,0,sizeof(next));
}
};
char s[35];
int n,maxnum;
void Insert(Trie *root,char s[])
{
Trie *p = root;
for(int i = 0; s[i]; i++)
{
int j = s[i] - '0';
if(p->next[j] == NULL) p->next[j] = new Trie();
p = p->next[j];
}
p->n++;
if(p->n > maxnum) maxnum = p->n;
}
void Delete(Trie *root) //释放空间
{
for(int i = 0; i < 10; i++)
{
if(root->next[i])
{
Delete(root->next[i]);
delete(root->next[i]);
}
}
root = NULL;
}
int main()
{
//freopen("1002.txt","r",stdin);
while(scanf("%d",&n) != EOF)
{
int i;
Trie *root = new Trie();
maxnum = 0;
while(n--)
{
char c[35];
scanf("%s",s);
for(i = 0; s[i]; i++) //去前导0
if(s[i]!='0' || !s[i+1])
{
strcpy(c,&s[i]);
break;
}
Insert(root,c);
}
printf("%d\n",maxnum);
Delete(root);
}
return 0;
}