poj 1002 487-3279 // hoj 1347 487-3279

/*

 

trie树建立,然后递归打印所有的电话号码(统计次数大于1的打印出来),内存在HOJ上爆了,应该是递归时爆栈了。。。第二个代码两个都是可以AC的,要注意POJ上是只输入一个案例,HOJ上输入多个样例

39268K 938MS

 第二个

14232K 891MS

*/

#include <cstdio>

#include <cstring>

#include <iostream>

using namespace std;

#define X 125

char s[X];

int n;

struct trie

{

    int id;

    trie *p[10];

    char s[X];

    trie()

    {

        id = 0;

        memset(p,NULL,sizeof(p));

    }

}root;

int ma[] = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,-1,7,7,8,8,8,9,9,9,-1};

void insert(trie *r)

{

    int c,cnt = 0;

    char temp[10];

    for(int i=0;s[i];i++)

    {

        c = -1;

        if(s[i]>='0'&&s[i]<='9')

            c = s[i]-'0';

        else if(s[i]>='A'&&s[i]<='Z')

            c = ma[s[i]-'A'];

        if(c==-1)

            continue;

        temp[cnt++] = c+'0';

        if(r->p[c]==NULL)

            r->p[c] = new trie();

        r = r->p[c];

    }

    temp[7] = '\0';

    strcpy(r->s,temp);

    r->id++;

}

bool flag;

void print(trie *r)

{

    for(int i=0;i<10;i++)

    {

        if(r->p[i]!=NULL)

            print(r->p[i]);

        if(r->id>1)

        {

            flag = true;

            for(int j=0;j<7;j++)

            {

                if(j==3)

                    printf("-");

                printf("%c",r->s[j]);

            }

            printf(" %d\n",r->id);

            r->id = 0;

        }

    }

}

int main()

{

    freopen("sum.in","r",stdin);

    freopen("sum.out","w",stdout);

    flag = false;

    trie *r = &root;

    scanf("%d",&n);

    for(int i=0;i<n;i++)

    {

        scanf("%s",s);

        insert(r);

    }

    print(r);

    if(!flag)

        cout<<"No duplicates."<<endl;

    return 0;

}

 

HOJ AC的代码,同样是trie树+递归打印

1.47s,21240k 

#include <cstdio>

#include <cstring>

#define X 125

char s[X];

int n;

struct trie

{

    int id;

    trie *p[10];

    trie()

    {

        id = 0;

        memset(p,NULL,sizeof(p));

    }

}root;

int ma[] = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,-1,7,7,8,8,8,9,9,9,-1};

void insert(trie *r)

{

    int c;

    for(int i=0;s[i];i++)

    {

        c = -1;

        if(s[i]>='0'&&s[i]<='9')

            c = s[i]-'0';

        else if(s[i]>='A'&&s[i]<='Z')

            c = ma[s[i]-'A'];

        if(c==-1)

            continue;

        if(r->p[c]==NULL)

            r->p[c] = new trie();

        r = r->p[c];

    }

    r->id++;

}

bool flag;

void print(trie *r,char temp[],int cnt)

{

    for(int i=0;i<10;i++)

    {

        if(r->p[i]!=NULL)

        {

            temp[cnt] = '0'+i;

            print(r->p[i],temp,cnt+1);

        }

        if(r->id>1)

        {

            flag = true;

            for(int j=0;j<7;j++)

            {

                if(j==3)

                    printf("-");

                printf("%c",temp[j]);

            }

            printf(" %d\n",r->id);

            r->id = 0;

        }

        else

            r->id = 0;

    }

}

int main()

{

    freopen("sum.in","r",stdin);

    freopen("sum.out","w",stdout);

    int t;

    scanf("%d",&t);

    while(t--)

    {

        flag = false;

        trie *r = &root;

        scanf("%d",&n);

        for(int i=0;i<n;i++)

        {

            scanf("%s",s);

            insert(r);

        }

        char temp[10];

        print(r,temp,0);

        if(!flag)

            printf("No duplicates.\n");

        if(t)

            printf("\n");

    }

    return 0;

}

 

你可能感兴趣的:(poj)