• 修改第二次作业-个人项目


    [第二次作业链接] https://www.cnblogs.com/031602401chl/p/9615496.html

    第一次作业的检测结果都超时了所以改了一下之前的代码。
    核心代码

    //分割单词 并存入map部分
    map<string, int> ma;
    for (int y = 0; y < temp.size(); ) {
    			if (temp[y]<48||((57<temp[y])&&(temp[y]<97))||temp[y]>122)
    			{
    				if (y > temp.size())break;
    				y++;
    				continue;
    			}
    			else {//发现一个单词
    				while ((temp[y] >= 'a'&&temp[y] <= 'z') || (temp[y] >= '0'&&temp[y] <= '9')) {
    					word.append(temp, y, 1);
    					y++;
    					if (y > temp.size())break;
    				}
    				if (Judge(word)) {
    					ma[word] ++;
    				}
    				word.clear();
    			}
    		}
    
    //初始化vector ,以及借助PAIR,将map中数据带入vector中,并改写sort的cmp函数部分。
    map<string, int> ma;
    typedef pair<string, int> PAIR;
    bool cmp(PAIR a, PAIR b)
    {
    	if (a.second > b.second)           
    	  return true;
    	if (a.second < b.second)    
    	  return false;
    	if (a.first==b.first)   
    	  return true;
    	return false;
    }
    vector<PAIR> name_times_vec(ma.begin(), ma.end());
    		sort(name_times_vec.begin(), name_times_vec.end(), cmp);
    
    对比之前的代码 引入了append函数的使用,map的使用,vector的使用。
    引入map的使用,方便了单词的储存以及计数。
    再使用sort加快排序速度。
    

    运行结果


    能在20s内完成(虽然还是算超时)

  • 相关阅读:
    Linux 工具命令
    Js的一些工具方法
    使用curl测试网络通信
    python 创建XML
    Nginx 使用Lua脚本
    lua 安装cjson
    3对象和类_动手动脑
    10.5
    10.4动手动脑
    10.2
  • 原文地址:https://www.cnblogs.com/031602401chl/p/9665968.html
Copyright © 2020-2023  润新知