forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclone.cpp
81 lines (81 loc) · 1.12 KB
/
clone.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// 2008-08-28
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#ifdef ONLINE_JUDGE
#define pc putchar_unlocked
#else
#define pc putchar
#endif
using namespace std;
int mx;
const int id[]={0,0,1,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,3};
struct TrieNode
{
int count;
TrieNode* link[4];
};
TrieNode memory[2000000];
int top=1;
TrieNode* root;
short a[20001];
int M;
void insert(char* s)
{
TrieNode* cur=root;
int i;
char c;
for (i=0; i<M; i++)
{
c=id[s[i]-'A'];
if (cur->link[c])
cur=cur->link[c];
else
{
cur->link[c]=memory+top++;
cur=cur->link[c];
memset(cur,0,20);
}
}
a[cur->count++]--;
if (cur->count>mx)
a[++mx]=1;
else
a[cur->count]++;
}
void output(int x)
{
int z=1;
while ((z<<1)+(z<<3)<=x)
z=(z<<1)+(z<<3);
while (z)
{
pc(x/z%10+'0');
z/=10;
}
pc('\n');
}
char s[500000];
int main()
{
int N,i,j;
for(;;)
{
scanf("%d %d",&N,&M);
if (!N)
return 0;
root=memory+top++;
memset(root,0,20);
mx=0;
a[0]=N;
fread(s,(M+1)*N,1,stdin);
for (i=0; i<N; i++)
insert(s+(M+1)*i+1);
for (i=1; i<=mx; i++)
output(a[i]);
for (i=mx+1; i<=N; i++)
{
pc('0');
pc('\n');
}
}
}