hdu 1075 What Are You Talking About (Trie树,水题)

小记:这题练的是对Trie树的运用,对火星文字符串建树,每个火星单词标记它为对应英文单词。这题就是对Trie节点多添加一个信息,然后是字符串的处理,练你码字的能力和基础了。1A,很幸运。 这题因为只有一个测试用例 所以可以不释放开辟的空间,因此速度会快点,我不释放是250MS,释放是300多MS。


贴上释放代码:

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>

using namespace std;

#define MAX 26

typedef struct Node{
    int isStr;
    char s[10];
    struct Node *next[MAX];
    Node():isStr(0){
        memset(s,0,sizeof(s));
        memset(next, NULL, sizeof(next));
    }
    ~Node(){
        for(int i = 0;i < MAX; ++i)
            if(next[i] != NULL)
                delete next[i];
    }
}TrieNode,*Trie;

Trie root;
char s1[3001];

void Insert(char *s,char *str){
    TrieNode *p = root;

    while(*s){
        if(p ->next[*s-'a'] == NULL){
            p ->next[*s-'a'] = new TrieNode;
        }
        p = p ->next[*s-'a'];
        s++;
    }
    p->isStr = 1;
    strcpy(p->s,str);
    return ;
}

int find(char *s){
    TrieNode *p = root;
    while(*s){
        if(p->next[*s - 'a'] != NULL){
            p = p->next[*s - 'a'];
            s++;
        }
        else return 0;
    }
    if(p->isStr){
        printf("%s",p->s);
        return 1;
    }
    return 0;
}

int main() {
    //freopen("f:\\in1.txt","r",stdin);
    //freopen("f:\\out1.txt","w",stdout);

    int num = 0, k, i, j, flag, len;
    char s2[10], c;
    root = new TrieNode;
    scanf("%s",s2);
    while(scanf("%s",s2)){
        if(s2[0] == 'E')break;
        scanf("%s",s1);
        Insert(s1,s2);
    }
    scanf("%s",s1);
    getchar();
    while(gets(s1)){
        if(s1[0] == 'E')break;
        len = strlen(s1);
        for(int i = 0; i < len; i++){

            if(s1[i] >= 'a' && s1[i]  <= 'z'){
                s2[num++] = s1[i];
            }
            else {
                s2[num] = '\0';
                if(!find(s2))
                    printf("%s",s2);
                num = 0 ;
                printf("%c",s1[i]);
            }
        }
        putchar('\n');
    }
    delete root;
}


2014.4.29 (ss数组必须开到这么大)

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>

using namespace std;

#define mst(a,b) memset(a,b,sizeof(a))
#define eps 10e-8

const int MAX_ = 3010;
const int MAX = 26;
const int N = 500010;
const int INF = 0x7fffffff;

typedef struct Node{
    int isStr;
    struct Node *next[MAX];

    Node():isStr(-1){
        memset(next, NULL, sizeof(next));
    }
    ~Node(){
        for(int i = 0;i < MAX; ++i)
            if(next[i] != NULL)
                delete next[i];
    }
}TrieNode,*Trie;

Trie root;
char s[MAX_], str[MAX_], ss[N][15];


void Insert(char *s, int num){
    TrieNode *p = root;
    while(*s){
        if(p ->next[*s-'a'] == NULL){
            p ->next[*s-'a'] = new TrieNode;
        }
        p = p ->next[*s-'a'];
        s++;
    }
    p->isStr = num;
}

int find(char *s){
    int i = 0;
    TrieNode *p=root;
    while(*s){
        if(p->next[*s-'a'] == NULL)return -1;
        p = p->next[*s-'a'];
        s++;
    }

    return p->isStr;
}

int main() {

    int ans, cnt, len, T, n;
    cnt = 0;
    root = new TrieNode;
    scanf("%s",s);
    while(scanf("%s",s) && s[0] != 'E'){
        scanf("%s",str);
        strcpy(ss[cnt], s);
        Insert(str, cnt);cnt++;
    }
    scanf("%s",s);
    getchar();
    cnt = 0;
    while(gets(s)&& s[0] != 'E'){
        len = strlen(s);
        for(int i = 0; i < len; ++i){
            if(s[i] >= 'a' && s[i] <= 'z'){
                str[cnt++] = s[i];
            }
            else {
                str[cnt] = '\0';
                n = find(str);
                if(n == -1){
                    printf("%s", str);
                }
                else printf("%s", ss[n]);

                cnt = 0;
                printf("%c", s[i]);
            }
        }
        putchar('\n');

    }
    delete root;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值