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 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 into English. Can you help him?
 
Input
The problem has only one test case, the test case consists of two parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string "START", this string should be ignored, then some lines follow, each line contains two strings, the first one is a word in English, the second one is the corresponding word in Martian's language. A line with a single string "END" indicates the end of the directory part, and this string should be ignored. The book part starts with a single line contains a string "START", this string should be ignored, then an article written in Martian's language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should translate it and write the new word into your translation, if you can't find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. Space(' '), tab('\t'), enter('\n') and all the punctuation should not be translated. A line with a single string "END" indicates the end of the book part, and that's also the end of the input. All the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters.
 
Output
In this problem, you have to output the translation of the history book.
 
Sample Input
START from fiwo hello difh mars riwosf earth fnnvk like fiiwj END START difh, i'm fiwo riwosf. i fiiwj fnnvk! END
 
Sample Output
hello, i'm from mars. i like earth!
#include <iostream>
#include <cstdio>
#include <string>
#include <map>
#include <cstring>
#include <cctype>
using namespace std;
map <string , string > mymap;
int main()
{
    int i,j,k;
    string mar,earth,s;
    cin>>s;//start,不需要吸收换行符,因为上下均为cin 
    while(cin>>earth)
    {
        if(earth == "END")
            break;
        cin>>mar;
        mymap[mar]=earth;
    }
    //cin>>s;//start,若是getline则需要 吸收上一次的换行符 
    //getchar(); 
    getline(cin, s);	// filter '\n'
	getline(cin, s);
    string sec,code;
    while(getline(cin,sec))
    {
        if(sec=="END")
            break;
        for(i=0;i<sec.size();i++)
        {
            if(isalpha(sec[i]))
            {
                code+=sec[i];
                if(!isalpha(sec[i+1]))
                {
                    if(mymap[code]!="")//map返回一个string 
                        cout<<mymap[code];
                    else
                        cout<<code;//原来写成了sec,和sec[i] 
                    code.clear();
                }
            }
            else
                cout<<sec[i];
        }
        cout<<endl;
    }
    return 0;
}
                
        
    
//RE
#include <stdio.h>   
#include <string.h> 
#include <malloc.h>
#include <stdlib.h>  
#include <ctype.h>//isalpha
const int N = 26;
typedef struct Node    
{   
    int cnt;   
    Node *child[26];  
    char str_temp[12];  
}Node; 
Node *root;    
void init()
{//对根节点初始化,根节点不存储任何数据
    root =(Node *)malloc(sizeof(Node)); 
    for(int i = 0; i < N; i++)
        root->child[i] = NULL;
    root->cnt = 0; 
    memset(root->str_temp,0,sizeof(root->str_temp));
} 
void insert(char *str,char *str1)
{
    Node *current, *newnode;   
    int i, j,index;
    int len = strlen(str);    
    if(len == 0)
        return;//无须插入的情况
    current = root;
    for(i = 0; i < len; i++)
    {
        index = str[i]-'a';
        if(current->child[index]!=NULL)
        {//子树存在
            current = current->child[index];
            current->cnt++; 
        } 
        else
        {//子树不存在的情况 
            newnode =(Node *)malloc(sizeof(Node)); 
            memset(newnode->str_temp,0,sizeof(newnode->str_temp));
            for(j = 0; j < N; j++)
                newnode->child[j] = NULL;
            newnode->cnt = 1;            
            current->child[index] = newnode;//这一句不可少 
            current = newnode; //current = current->child[index]
        }
    }
    strcpy(current->str_temp,str1);
}
void search( char *str)   
{ 
    
    int i, j, k, index, len;  
    Node *current, *newnode;    
    current = root; 
    len = strlen(str);
    if(len==0)
        return ;
   // printf("len=%d\n",len);
    for(i=0;i<len;++i )   
    {   
        index=str[i]-'a';   
        if(current->child[index]==NULL) 
        { 
           printf("%s",str); 
            return;
        }   
        current=current->child[index];   
    }
    if(i==len)
    {
        if(current->str_temp[0]!='\0')
        //如果找到了,且相应节点上存放着字符串,则输出,即所查找的单词不是前缀
             printf("%s",current->str_temp);
        else
            printf("%s",str); 
        return ;
    }                
}               
int main()   
{   
    char str0[3010],str[10],str3[3010],str1[12],str2[12]; 
    int i=0,j,k,r;
    init();  
    gets(str);//吸收掉start 
    while(1)
    {
        memset(str1,0,sizeof(str1));
        memset(str2,0,sizeof(str2));
        scanf("%s",str1);
        if(strcmp(str1,"END")==0)
            break;
        scanf("%s",str2);
        insert(str2,str1);                                       
    } 
    fflush(stdin);
    memset(str,0,sizeof(str));
    gets(str);//吸收掉start    
    fflush(stdin);
    while(gets(str0),strcmp(str0,"END"))   
    { 
        j=0;
        for(i=0;str0[i]!='\0';i++)
        {
            if(isalpha(str0[i]))
            {
                str3[j]=str0[i];
                j++;
            }
            else 
            {
                str3[j]='\0';    
                //如果search函数里有判断字符串长度的就需要加上if(j!=0) 
                    search(str3);
                putchar(str0[i]); 
                memset(str3,0,sizeof(str3));  
                j=0;             
            }
        }  
        printf("\n");      
        memset(str0,0,sizeof(str0));
        memset(str3,0,sizeof(str3));
    } 
    printf("\n");
    return 0;   
}  
 
原文地址:https://www.cnblogs.com/hxsyl/p/2633022.html