poj 2001 Shortest Prefixes

哎,开始理解错题意了,还以为找的是已经存在的字符串,  搞得连个测试数据都过不了,结果看了下别人的博客,才发现又会错了意,哎,做题须谨慎啊。

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int N = 3010;

struct Node{
int flag;
int count;
Node *next[26];
}root;

int tot;
Node node[N];
char word[1010][25];

void insert(char *str,int n)
{
    Node *p = &root;
    while(*str)
    {
        if(p->next[*str - 'a'] == NULL)
        {
            memset(node[tot].next, NULL, sizeof(node[tot].next));
            node[tot].flag = 0;
            node[tot].count = 0;
            p->next[*str - 'a'] = &node[tot++];
        }
        p = p->next[*str - 'a'];
        p->count++;
        str++;
    }
    p->flag = n;
}

void search(char *str)
{
    Node *p = &root;
    while(*str&&p)
    {
        if(p->count==1)
          break;
        printf("%c",*str);
        p = p->next[*str - 'a'];
        str++;
    }
    putchar(10);
}

int main(void)
{
    int n = 0;
    tot = 0;
    memset(root.next, NULL, sizeof(root.next));
    while(scanf("%s",word[++n])!=EOF)
    {
        //cout<<"ok"<<endl;
        insert(word[n], n);
    }
    for(int i=1; i<=n; ++i)
    {
        printf("%s ",word[i]);
        search(word[i]);
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值