zoukankan      html  css  js  c++  java
  • hdu 1075 What Are You Talking About

    乱写的代码。之所以乱写,是为了复习字典树和map

    要注意文章单词间可能有多个空格和字符

    #include<iostream>
    #include<cstdio>
    #include<map>
    #include<cstring>
    #include<string>
    using namespace std;
    map<string,string>mapp;
    string str;
    struct stu
    {
    	int flag;
    	stu* a[26];
    	stu()
    	{
    		flag=0;
    		for(int i=0;i<26;i++) a[i]=NULL;
    	}
    };
    stu* root=new stu();
    void build(stu *root,int cnt)
    {
    	int x=str[cnt]-'a';
    	if(root->a[x]==NULL) root->a[x]=new stu();
    	root=root->a[x];
    	if(cnt==str.size()-1)
    	{
    		root->flag=1;
    		return;
    	}
    	build(root,cnt+1);
    }
    string find(stu* root,int cnt)
    {
    	int x=str[cnt]-'a';
    	if(x<0||x>25) return str;
    	if(root->a[x]==NULL) return str;
    	root=root->a[x];
    	if(cnt==str.size()-1)
    	{
    		if(root->flag==1) return mapp[str];
    		else return str;
    	}
    	else return find(root,cnt+1);
    }
    int main()
    {
    	//cin.sync_with_stdio(false);
    	string cmd;
    	cin>>cmd;
    	while(cin>>cmd&&cmd!="END")
    	{
    		cin>>str;
    		mapp[str]=cmd;
    		build(root,0);
    	}
    	cin>>cmd;
    	getchar();
    	while(getline(cin,cmd)&&cmd!="END") 
    	{
    		for(int i=0;i<cmd.size();i++)
    		{
    			str="";
    			while(cmd[i]>='a'&&cmd[i]<='z') str+=cmd[i++];
    			cout<<find(root,0)<<cmd[i];
    		}
    		cout<<endl;
    	}
    	return 0;
    } 


  • 相关阅读:
    发送xml请求数据,返回数据怎么获取
    laravel打印sql语句
    布隆过滤器-使用场景的思考
    sql性能优化
    JS Date.parse() 函数详解
    vuejs中的watch监听属性
    JS正则test()方法
    golang实现简单线程池
    golang map实现set
    golang init函数
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/7224590.html
Copyright © 2011-2022 走看看