m0_67597520 2022-05-09 21:19 采纳率: 86.4%
浏览 59

老是报运行超时,可以简化,或者优化吗?

输入:
i have brought #peace #freedom #justice and #security to my new empire
输出
4
#freedom 1
#justice 1
#peace 1
#security 1


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using std::cin;
using std::cout;
using std::vector;
using std::string;
using std::find;
class Solution{
public:
    static vector<string> tags(vector<string> inputs){
        vector<string> tag;
        for(string x:inputs){
            if(x[0]=='#'&&x.size()>1&&x.find('#',1)==x.npos){
                tag.push_back(x);
            }
        }
        return tag;
    }
    static int tag_num(vector<string>tag){
        int num=0;
        int flag=0;
        for(string x:tag){
            if(count(tag.begin()+flag, tag.end(), x)<=1){
                num++;
            }
            flag++;
        }
        return num;
    }
    static int single(string x,vector<string>tag){
        int num=0;
        for(string y:tag){
            if(x.compare(y)==0){
                num++;
            }
        }
        return num;
    }
};
int main(int argc, char const *argv[])
{
    string str;
    vector<string> orn;
    while(cin>>str){
       //for(str;cin>>str){ 
        orn.push_back(str);
       
    }
    
    vector<string> tags(Solution::tags(orn));
    cout<<Solution::tag_num(tags)<<std::endl;
    vector<string> tags_new(tags);
    sort( tags_new.begin(), tags_new.end() );
    tags_new.erase( unique( tags_new.begin(), tags_new.end() ), tags_new.end() );
    for(auto x:tags_new){
        cout<<x<<" "<<Solution::single(x,tags)<<std::endl;
    }
    return 0;
}
  • 写回答

3条回答 默认 最新

  • SmallAntJ 2022-05-10 00:18
    关注

    47行的while死循环了,请把具体题目要求发出来。

    评论

报告相同问题?

问题事件

  • 创建了问题 5月9日

悬赏问题

  • ¥20 ARM64架构下SSCMS部署问题
  • ¥20 请教如何让移动端的热区自适应
  • ¥30 为什么深度学习网络训练会出现训练集和测试集准确率和损失都不变的情况
  • ¥15 (标签-3D|关键词-色模型)
  • ¥15 发现在MSVS2022中”#pragma once“不起作用
  • ¥15 实时声纹降噪算法进行无关说话人语音抑制
  • ¥100 adb针对解决手机锁屏问题(相关搜索:手机锁屏)
  • ¥15 发现哔哩哔哩的粉丝只能显示前5页能不能爬出来
  • ¥15 oracle数据库求解
  • ¥15 c++ websocketpp连接币安 有报错,有偿求解