zoukankan      html  css  js  c++  java
  • HDU1075-What Are You Talking About

    What Are You Talking About

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)
    Total Submission(s): 20751    Accepted Submission(s): 6881


    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(' '), enter(' ') 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!
    Hint
    Huge input, scanf is recommended.
     
    题目大意:
    相当于就是给了原文和对应的译文,然后给出例句,输出真正想要说的内容。都是以START开始和END结束。

    解题思路:
    用map做的话很简单,要是需要的话后面在补上。这里我是用字典树做的,缺点就是容易超内存。麻烦的地方时就是树需要自己建。下面的代码里面注释写的已经很详细了,所以就不再说了。



    源代码:
    <span style="font-size:18px;">
    #include<iostream>
    #include<algorithm>
    #include<string>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    using namespace std;
    
    const int MAXN = 26;
    const int MAXM = 15;
    const int MAXS = 3005;
    
    struct Trie//名字不用起多,按书上的写太累赘
    {
    	char s[MAXM];
    	Trie *next[MAXN];
    };
    Trie *root;//紧随其后,声明一个
    
    void init()
    {
    	root = (Trie*)malloc(sizeof(Trie));
    	strcpy(root->s, "");
    	for (int i = 0; i<MAXN; i++)
    	{
    		root->next[i] = NULL;
    	}
    }
    
    void CreateTire(char str[], char ss[])//不许要传整棵树了
    {
    	//这里需要p来对roor进行操作,因为会有迭代过程
    	//如果root参与运算的话,root=root->next[i],root的值就被改变了
    	Trie *p = root, *temp;
    	int len = strlen(ss);
    	for (int i = 0; i<len; i++)
    	{
    		int id = ss[i] - 'a';
    		if (p->next[id] == NULL)//说明还没有访问过
    		{
    			//对临时temp进行初始话,同root的初始化一样
    			temp = (Trie*)malloc(sizeof(Trie));
    			strcpy(temp->s, "");
    			for (int j = 0; j<MAXN; j++)
    			{
    				temp->next[j] = NULL;
    			}
    			p->next[id] = temp;
    			p = p->next[id];
    		}
    		else//访问过继续向下走
    		{
    			p = p->next[id];
    		}//到最后了,把需要翻译的字符串拷贝进来
    		if (i == len - 1)
    		{
    			strcpy(p->s, str);
    		}
    	}
    	//建树成功
    }
    
    string FindTrie(char ss[])//写string,不要写成char*,会返回地址
    {
    	string ans = "";
    	Trie *p = root;
    	int len = strlen(ss);
    	for (int i = 0; i<len; i++)
    	{
    		int id = ss[i] - 'a';
    		if (p->next[id] == NULL)
    		{
    			ans = "";//没有的话就把空串拷贝进去
    			return ans;
    		}
    		else
    		{
    			p = p->next[id];//有的话继续向下
    		}
    	}
    	//假设ss对应的没有保存,因为ss可能是另外一个的前缀,这个时候p->next[id]也不为空
    	//但是拷贝进来的还是空串,不用担心
    	ans = (string)p->s;
    	return ans;
    }
    
    void getData()
    {
    	char s[MAXM];
    	scanf("%s", s);//先把start读进来
    	while (~scanf("%s", s))
    	{
    		if (s[0] == 'E')//读到end退出
    			break;
    		char ss[MAXM];
    		scanf("%s", ss);
    		CreateTire(s, ss);
    	}
    }
    
    void slove()
    {
    	string s;//因为整行读取还是string方便
    	cin >> s;
    	getline(cin, s);
    	while (getline(cin, s))
    	{
    		if (s[0] == 'E')
    			break;
    		int st;//开始结束的下标
    		st = 0;
    		string res = "";
    		s += ".";//末尾加一个不是小写字母的字符方便处理
    		int len = s.length();
    		for (int i = 0; i<len; i++)
    		{
    			if (s[i]>'z'||s[i]<'a')//不属于小写字母,逻辑或的关系
    			{
    				char temp[MAXM];
    				int index = 0;
    				for (int j = st; j<i; j++)
    				{
    					temp[index] = s[j];
    					index++;
    				}
    				temp[index] = '';//''结尾
    				string ss = FindTrie(temp);
    				if (ss=="")//是空串
    				{
    					res += (string)temp;//保留原值追加
    				}
    				else
    				{
    					res += ss;
    				}
    				//寻找下一个小写字母开头的位置赋值st
    				st = i;
    				while (s[i]>'z'||s[i]<'a'&&i<len-1)
    				{
    					i++;
    				}
    				index = 0;
    				//把中间不是小写字符的字符串原值保留
    				for (int j = st; j < i; j++)
    				{
    					temp[index] = s[j];
    					index++;
    				}
    				temp[index] = '';
    				res += (string)temp;
    				st = i;
    				
    			}
    		}
    		cout << res << endl;
    	}
    	delete(root);
    }
    int main()
    {
    	init();
    	getData();
    	slove();
    	return 0;
    }
    </span>



  • 相关阅读:
    Java操作redis
    ExtJs6编译之后上线报错无法查看到的解决方法
    使用idea开发工具,nginx服务部署Extjs6,SpringBoot项目到服务器
    跨域拦截Access-Control-Allow-Origin设置多个origin
    ExtJs6内嵌iframe,nginx部署本地前台文件
    基于resteasy,Base64码上传文件
    resteasy上传单个文件/多个文件到阿里云服务器
    win7 系统保留分区 BCDedit
    Ubuntu Linux系统下apt-get命令详解
    Linux 查看 硬件配置
  • 原文地址:https://www.cnblogs.com/lemonbiscuit/p/7776051.html
Copyright © 2011-2022 走看看