动物统计加强版
时间限制:
3000 ms | 内存限制:
150000 KB
难度:
4
-
描述
-
在美丽大兴安岭原始森林中存在数量繁多的物种,在勘察员带来的各种动物资料中有未统计数量的原始动物的名单。科学家想判断这片森林中哪种动物的数量最多,但是由于数据太过庞大,科学家终于忍受不了,想请聪明如你的ACMer来帮忙。
-
输入
-
第一行输入动物名字的数量N(1<= N <= 4000000),接下来的N行输入N个字符串表示动物的名字(字符串的长度不超过10,字符串全为小写字母,并且只有一组测试数据)。
输出
-
输出这些动物中最多的动物的名字与数量,并用空格隔开(数据保证最多的动物不会出现两种以上)。
样例输入
-
10 boar pig sheep gazelle sheep sheep alpaca alpaca marmot mole
样例输出
-
sheep 3
来源
- [陈玉 张云聪]原创 上传者
- 陈玉
-
第一行输入动物名字的数量N(1<= N <= 4000000),接下来的N行输入N个字符串表示动物的名字(字符串的长度不超过10,字符串全为小写字母,并且只有一组测试数据)。
一般排序超时,,就用字典树了-.-
-.-记下最大个数及对应的字符串.....
错误代码:TLE,,,,sort排序时间是nlog2 n 是10的8次方左右。。。
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
struct node{
char hh[12];
int ll;
}ch[4000100];
bool cmp(node xx,node yy)
{
if (xx.ll!=yy.ll)
return xx.ll<yy.ll;
for (int i=0;i<xx.ll;i++)
{
if (i==xx.ll-1)
return xx.hh[i]<yy.hh[i];
else
{
if (xx.hh[i]!=yy.hh[i])
return xx.hh[i]<yy.hh[i];
}
}
}
int main()
{
int n;
// while (~scanf("%d",&n))
// {
scanf("%d",&n);
for (int i=0;i<n;i++)
{
scanf("%s",ch[i].hh);
ch[i].ll=strlen(ch[i].hh);
}
sort(ch,ch+n,cmp);
int s=0,ss=0,lp=0,kk=0;
strcpy(ch[n].hh,"11111111111");
ch[n].ll=12;
for (int i=1;i<=n;i++)
if (strcmp(ch[i-1].hh,ch[i].hh))
{
s=i-lp;
if (s>ss)
{
ss=s;
kk=lp;
}
lp=i;
}
// for (int i=0;i<=n;i++)
// printf("666 %s\n",ch[i].hh);
printf("%s %d",ch[kk].hh,ss);
// }
return 0;
}
错误代码:
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
struct trie{
struct trie *chilren[26];
int lplp;
};
char *ko;//指针
int s;
struct trie root;
void jia(char xx[])
{
struct trie *kk;
kk=&root;
int ll=strlen(xx);
for (int i=0;i<ll;i++)
{
if (!kk->chilren[xx[i]-'a'])
{
kk->chilren[xx[i]-'a']=new trie;
memset(kk->chilren[xx[i]-'a'],0,sizeof(trie));
kk->lplp=0;//这是第二个错误....wuwuwu
}
if (i==ll-1)
{
kk->lplp=kk->lplp+1;
if (s<kk->lplp)
{
s=kk->lplp;
ko=xx;
}
}
kk=kk->chilren[xx[i]-'a'];
}
}
int main()
{
int n;scanf("%d",&n);
s=0;
char lll[12];
for (int i=0;i<26;i++)
root.chilren[i]=0;
root.lplp=0;
while (n--)
{
scanf("%s",lll);
jia(lll);
}
printf("%s %d\n",ko,s);//用指针是最大错误、、因为我指向的是lll,最后一定输出最后那个lll.....
return 0;
}
AC代码:
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
struct trie{
struct trie *chilren[26];
int lplp;
};
char ko[12];
int s;
struct trie root;
void jia(char xx[])
{
struct trie *kk;
kk=&root;
int ll=strlen(xx);
for (int i=0;i<ll;i++)
{
if (kk->chilren[xx[i]-'a']==NULL)
{
kk->chilren[xx[i]-'a']=new trie();
memset(kk->chilren[xx[i]-'a'],0,sizeof(trie));
kk->chilren[xx[i]-'a']->lplp=0;
}
kk=kk->chilren[xx[i]-'a'];
if (i==ll-1)
{
kk->lplp=kk->lplp+1;
if (s<kk->lplp)
{
s=kk->lplp;
strcpy(ko,xx);
}
}
}
}
int main()
{
int n;scanf("%d",&n);
s=0;
char lll[12];
for (int i=0;i<26;i++)
root.chilren[i]=0;
root.lplp=0;
while (n--)
{
scanf("%s",lll);
jia(lll);
}
printf("%s %d\n",ko,s);
return 0;
}