简单的字典树水题,统计为单词前缀的单词数,取最大,注意要去前0,具体原理还是自己想想吧,最大数决定了最大分组。
诶 PKU月赛之后 我发现我只会水的 ..............
#include
<
iostream
>
#include < queue >
using namespace std;
struct node
{
long num;
int next[ 10 ];
void init(){memset(next, - 1 , sizeof (next));num = 0 ;}
}s[ 200000 ];
int p;
char a[ 100 ];
inline void preprocess(){s[p = 0 ].init();}
long lmax;
inline void insert()
{
int index = 0 ;
// ++s[index].num;
bool begin = false ;
for ( int i = 0 ;a[i] != ' \0 ' ; ++ i)
{
if (a[i] != ' 0 ' )
{
begin = true ;
}
if ( ! begin)
{
continue ;
}
int x = a[i] - ' 0 ' ;
if (s[index].next[x] ==- 1 )
{
s[ ++ p].init();
s[index].next[x] = p;
}
index = s[index].next[x];
}
s[index].num ++ ;
if (s[index].num > lmax)
{
lmax = s[index].num;
}
}
int main()
{
long n;
while (scanf( " %ld " , & n) != EOF)
{
lmax = 0 ;
getchar();
preprocess();
long i;
for (i = 0 ;i < n; ++ i)
{
gets(a);
insert();
}
printf( " %ld\n " ,lmax);
}
return 0 ;
}
#include < queue >
using namespace std;
struct node
{
long num;
int next[ 10 ];
void init(){memset(next, - 1 , sizeof (next));num = 0 ;}
}s[ 200000 ];
int p;
char a[ 100 ];
inline void preprocess(){s[p = 0 ].init();}
long lmax;
inline void insert()
{
int index = 0 ;
// ++s[index].num;
bool begin = false ;
for ( int i = 0 ;a[i] != ' \0 ' ; ++ i)
{
if (a[i] != ' 0 ' )
{
begin = true ;
}
if ( ! begin)
{
continue ;
}
int x = a[i] - ' 0 ' ;
if (s[index].next[x] ==- 1 )
{
s[ ++ p].init();
s[index].next[x] = p;
}
index = s[index].next[x];
}
s[index].num ++ ;
if (s[index].num > lmax)
{
lmax = s[index].num;
}
}
int main()
{
long n;
while (scanf( " %ld " , & n) != EOF)
{
lmax = 0 ;
getchar();
preprocess();
long i;
for (i = 0 ;i < n; ++ i)
{
gets(a);
insert();
}
printf( " %ld\n " ,lmax);
}
return 0 ;
}