输入
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){
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;
}
题目如下: