POJ 2945 trie树

Find the Clones
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 7704 Accepted: 2879
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' orT’.

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, and0’ 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.

题意:给出x个字符串,问你i个(i<=i<=x)相同字符串的个数,然后输出第i行代表有i个相同字符串的个数。

思路: 找的trie树题,自然就是trie树啦。好像别人有直接strcmp+sort+O(n)扫一遍过的,有用map过的,还有用hash的(Hash好像很有用的样子)。

第一次提交,,,

#include <cstdio>
#include <cstring>
using namespace std;
int x,y;
int a[20005];
struct trie
{
    int cnt;
    trie *next[26];
};
trie *root=new trie;
void insert(char ch[])
{
    trie *p=root,*newtrie;
    for(int i=0;ch[i]!='';i++)
    {
        if(p->next[ch[i]-'A']==0)
        {
            newtrie=new trie;
            for(int j=0;j<26;j++) newtrie->next[j]=NULL;
            newtrie->cnt=0;
            p->next[ch[i]-'A']=newtrie;
            p=newtrie;
        }
        else
            p=p->next[ch[i]-'A'];
    }
    p->cnt++;
    a[p->cnt]++;
    a[p->cnt-1]--;
}
int main()
{
    char ch[29];
    while(scanf("%d%d",&x,&y)&&x)
    {
        for(int i=0;i<26;i++) root->next[i]=NULL;
        root->cnt=0;
        for(int i=1;i<=x;i++)
        {
            scanf("%s",ch);
            insert(ch);
        }
        for(int i=1;i<=x;i++)
            printf("%d
",a[i]);
        for(int i=0;i<=x;i++)a[i]=0;
    }
} 

这里写图片描述
分析了一下原因,没有拆树导致用过了废了的内存没有回收

改了五分钟以后,第二版提交。

#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
int x,y;
int a[20005];
struct trie
{
    int cnt;
    trie *next[26];
};
trie *root=new trie;
void insert(char ch[])
{
    trie *p=root,*newtrie;
    for(int i=0;ch[i]!='';i++)
    {
        if(p->next[ch[i]-'A']==0)
        {
            newtrie=new trie;
            for(int j=0;j<26;j++) newtrie->next[j]=NULL;
            newtrie->cnt=0;
            p->next[ch[i]-'A']=newtrie;
            p=newtrie;
        }
        else
            p=p->next[ch[i]-'A'];
    }
    p->cnt++;
    a[p->cnt]++;
    a[p->cnt-1]--;
}
void dfs(trie *p)
{
    for(int i=0;i<26;i++)
    {
        if(p->next[i]!=NULL) dfs(p->next[i]);
        free(p->next[i]);
    }
}
int main()
{
    char ch[29];
    while(scanf("%d%d",&x,&y)&&x)
    {
        for(int i=0;i<26;i++) root->next[i]=NULL;
        root->cnt=0;
        for(int i=1;i<=x;i++)
        {
            scanf("%s",ch);
            insert(ch);
        }
        for(int i=1;i<=x;i++)
            printf("%d
",a[i]);
        for(int i=0;i<=x;i++)a[i]=0;
        dfs(root);
    }
}

还是很慢啊! 4.5s,19216K的memory。
这里写图片描述

原文地址:https://www.cnblogs.com/SiriusRen/p/6532499.html