POJ2945 Find the Clones[字典树简单题(多例输入注意删除)]

Find the Clones
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 6042   Accepted: 2263

Description

Doubleville, a small town in Texas, was attacked by the aliens. They have abducted some of the residents and taken them to the a spaceship orbiting around earth. After some (quite unpleasant) human experiments, the aliens cloned the victims, and released multiple copies of them back in Doubleville. So now it might happen that there are 6 identical person named Hugh F. Bumblebee: the original person and its 5 copies. The Federal Bureau of Unauthorized Cloning (FBUC) charged you with the task of determining how many copies were made from each person. To help you in your task, FBUC have collected a DNA sample from each person. All copies of the same person have the same DNA sequence, and different people have different sequences (we know that there are no identical twins in the town, this is not an issue).

Input

The input contains several blocks of test cases. Each case begins with a line containing two integers: the number 1 ≤ n ≤ 20000 people, and the length 1 ≤ m ≤ 20 of the DNA sequences. The next n lines contain the DNA sequences: each line contains a sequence of m characters, where each character is either `A', `C', `G' or `T'.

The input is terminated by a block with n = m = 0 .

Output

For each test case, you have to output n lines, each line containing a single integer. The first line contains the number of different people that were not copied. The second line contains the number of people that were copied only once (i.e., there are two identical copies for each such person.) The third line contains the number of people that are present in three identical copies, and so on: the i -th line contains the number of persons that are present in i identical copies. For example, if there are 11 samples, one of them is from John Smith, and all the others are from copies of Joe Foobar, then you have to print `1' in the first andthe tenth lines, and `0' in all the other lines.

Sample Input

9 6
AAAAAA
ACACAC
GTTTTG
ACACAC
GTTTTG
ACACAC
ACACAC
TCCCCC
TCCCCC
0 0

Sample Output

1
2
0
1
0
0
0
0
0

Hint

Huge input file, 'scanf' recommended to avoid TLE.

Source

 
 
 
 
第一边写完就感觉很好,不过交上去就MLE了,后来才发现没有删掉结构体。
第一次写析构delete,以后还是要多学习!
code:
 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 
 5 int n,m;
 6 int cnt[20010];
 7 
 8 struct node
 9 {
10     int cnt;
11     node *next[26];
12 }*p;
13 
14 void build(char str[],int k,node *head)
15 {
16     while(k<strlen(str))
17     {
18         if(head->next[str[k]-'A']!=NULL)
19         {
20             head->next[str[k]-'A']->cnt+=1;
21             head=head->next[str[k]-'A'];
22         }
23         else
24         {
25             head->next[str[k]-'A']=new node;
26             head=head->next[str[k]-'A'];
27             for(int i=0;i<26;i++)
28                 head->next[i]=NULL;
29             head->cnt=1;
30         }
31         if(k==strlen(str)-1)
32         {
33             if(head->cnt==1)
34                 cnt[1]++;
35             else
36             {
37                 cnt[head->cnt-1]--;
38                 cnt[head->cnt]++;
39             }
40         }
41         k++;
42     }
43 }
44 
45 void del(node *head)
46 {
47     if(head==NULL)
48         return;
49     for(int i=0;i<26;i++)
50     {
51         del(head->next[i]);
52         head->next[i]=NULL;
53     }
54     delete(head);
55     return;
56 }
57 
58 int main()
59 {
60     char str[30];
61     while(~scanf("%d%d",&n,&m),n||m)
62     {
63         memset(cnt,0,sizeof(cnt));
64         p=new node;
65         for(int i=0;i<26;i++)
66             p->next[i]=NULL;
67         for(int i=0;i<n;i++)
68         {
69             scanf("%s",str);
70             build(str,0,p);
71         }
72         del(p);
73         for(int i=1;i<=n;i++)
74             printf("%d\n",cnt[i]);
75     }
76     return 0;
77 }
原文地址:https://www.cnblogs.com/XBWer/p/2674437.html