hdu 1075 What Are You Talking About

点击打开hdu1075

题意:给定一个映射表的关系,给定每个单词的对应的映射的单词。现在给定一段字符串,要求如果单词能够翻译就进行翻译,否则原样输出,但是注意所有的\n,\r,空格以及所有的标点符号都是不翻译的。

思路:先对映射表的单词建立字典树,每个单词的尾节点标记这个单词所映射的单词的下标,那么对于给定的字符串,我们只要把所有的单词进行解析去字典树中查找即可。

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 30010;
const int N = 26;

struct Node{
   int index;
   Node* child[N];
};
char word[MAXN][10];
Node node[MAXN];
Node* root;
int pos;

Node* newNode(){
    node[pos].index = -1;
    for(int i = 0 ; i < N ; i++)
        node[pos].child[i] = NULL;
    return &node[pos++];
}

void insert(int index , char *str){
    Node* s = root;
    int len = strlen(str);
    for(int i = 0 ; i < len ; i++){
        int num = str[i]-'a';
        if(s->child[num] == NULL)
            s->child[num] = newNode();
        s = s->child[num];
    }
    s->index = index;
}

int search(char *str){
    Node* s = root;
    int len = strlen(str);
    for(int i = 0 ; i < len ; i++){
        int num = str[i]-'a';
        if(s->child[num] == NULL)
            return -1;
        s = s->child[num];
    }
    return s->index;
}

void solve(char *str){
    int len = strlen(str);
    char tmp[MAXN];
    int i = 0;
    while(i < len){
        if(!islower(str[i])){
            printf("%c" , str[i]);
            i++;
            continue;
        }
        memset(tmp , '\0' , sizeof(tmp));
        int j = i;
        int index = 0;
        while(j < len && islower(str[j]))
           tmp[index++] = str[j++];
        int cnt = search(tmp);
        if(cnt == -1)
            printf("%s" , tmp);
        else
            printf("%s" , word[cnt]);
        printf("%c" , str[j]);
        i = j+1;
    }
    puts("");
}

int main(){
    pos = 0;
    root = newNode();
    char str[MAXN];
    int index = 0;

    scanf("%s" , str);
    while(scanf("%s" , word[index])){
        if(strcmp(word[index] , "END") == 0)
            break;
        scanf("%s" , str);
        insert(index++ , str);
    }
    scanf("%s%*c" , str);
    while(gets(str)){
        if(strcmp(str , "END") == 0)
            break;
        solve(str);
    }
    return 0;
}
时间: 2024-08-30 15:52:51

hdu 1075 What Are You Talking About的相关文章

算法题:HDU 1075 What Are You Talking About(字典树学习题)

链表: http://acm.hdu.edu.cn/showproblem.php?pid=1075 题目: What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others) Total Submission(s): 7939    Accepted Submission(s): 2470 Problem Description Ign

HDOJ/HDU 1075 What Are You Talking About(字符串查找翻译~Map)

Problem Description Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book

HDU 1075

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)Total Submission(s): 7433    Accepted Submission(s): 2298 Problem Description Ignatius is so lucky that he met a Martian yesterday. But h

hdu 1527

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1527 hint:威佐夫博弈 基本类似于模板 #include <iostream> #include <cmath> #include <cstdio> using namespace std; const double q = (1 + sqrt(5.0)) / 2.0; // 黄金分割数 int Wythoff(int a, int b) { if (a > b)

hdu 2551 竹青遍野

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2551 hint:就是读懂题就行了 #include <iostream> #include <cstdio> using namespace std; typedef long long LL; LL data[1005]; int main() { data[0]=0; for(int i=1; i<1005; i++) data[i]+=data[i-1]+i*i*i; LL

hdu 2054 A == B?

http://acm.hdu.edu.cn/showproblem.php?pid=2054 此题巨坑,刚开始我以为是简单的水题,就用strcmp过, but错了,后来经过我苦思冥想,结果还有几组数据 0.0 和 0,1.000和1.0 , 但是我不太确定前面的0是不是有作用我还是写了,但是有人过的时候,前面的0没考虑比如: 002和2可能是相等的,也可能是不想等的所以不用判断,只能说明hdu数据不是很强啊,嘿嘿 代码如下: #include <iostream> #include <c

hdu 4430 Yukari&#039;s Birthday

点击打开链接hdu 4430 思路:枚举r+二分k 分析: 1 题目要求的是找到一组最小的r*k,如果r*k相同那么就找r最小的. 2 很明显k>=2,根据n <= 10^12,那么可以知道r的最大值r<50,所以只要枚举枚举r的值,然后二分k的大小找到所有的解,存入一个结构体里面,然后在对结构体排序,那么这样就可以得到最后的ans 3 注意题目说了中心点最多一个蜡烛,所以写二分的时候应该注意判断的条件: 4 还有可能计算得到结果超了long long直接变成负数所以应该对或则个进行判断

hdu 1238 Substrings

点击打开链接hdu 1238 思路:kmp+暴力枚举子串 分析: 1 题目要求找到一个子串x,满足x或x的逆串是输入的n个字符串的子串,求最大的x,输出x的长度 2 题目的n最大100,每一个字符串的最大长度为100,那么暴力枚举子串就是o(n^2)才10000肯定是不会超时的,但是由于这里涉及到了逆串的问题,所以我们应该还要求出n个子串的逆串,然后在求最大的x. 代码: #include<iostream> #include<algorithm> #include<cstd

hdu 1857 Word Puzzle

点击打开链接hdu 1857 思路:字典树 分析: 1 题目要求的是给定的单词第一个字母在这个矩形里面的最小的坐标 2 矩形的最大500*500,单词的来源有三个方向,并且单词的起点和终点在矩形之内都是可能的.所以的如果利用枚举矩形之内的单词,那么肯定是超内存的 3 所以我们必须考虑另一种的方法就是对单词进行建字典树,那么我们只要去枚举单词的可能的起点,然后进行查找相应的单词是不是在树上,如果是的话就标记一下当前的坐标. 4 注意由于单词的来源有三个方向,但是因为要求的如果下相同的情况下要求坐标