输入
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;
}
题目如下:
#include<iostream>
#include<unordered_map>
#include<string>
#include<sstream>
#include<algorithm>
using namespace std;
int main()
{
string line;
unordered_map<string, int> tags;
getline(cin, line);
int i = 0;
string word = "";
stringstream stream(line);
while (stream >> word)
if (word.size() > 1 && word[0] == '#' && count(word.begin(), word.end(), '#') == 1)tags[word]++;
cout << tags.size() << endl;
for (const auto tag : tags)
cout << tag.first << " " << tag.second << endl;
}
记录标签起始位置和结束位置就行
#include<stdio.h>
#include<string.h>
char t[100000];
int b[100000];
int e[100000];
int bp=0,ep=0;
int count[100000];
char lable[100000][150];
int p=0;
int main()
{
gets(t);
int len=strlen(t);
for(int i=0; i<len; i++)
{
if(t[i]=='#')
{
b[bp++]=i;
for(int j=i; j<=len; j++)
{
if(t[j]==' '||j==len)
{
e[ep++]=j;
break;
}
}
}
}
for(int i=0; i<bp; i++)
{
p=0;
for(int j=b[i]; j<e[i]; j++)
{
lable[i][p++]=t[j];
}
lable[i][p]='\0';
}
for(int i=0; i<bp; i++)
{
for(int j=0; j<bp; j++)
{
if(strcmp(lable[i],lable[j])==0&&i!=j)
{
count[i]++;
lable[j][0]='\0';
}
}
}
printf("%d\n",bp);
/*for(int i=0; i<bp; i++)
{
for(int j=b[i]; j<=e[i]; j++)
{
printf("%c",t[j]);
}
printf("%d",count[i]);
printf("\n");
}*/
for(int i=0; i<bp; i++)
{
if(lable[i][0]!='\0')
{
printf("%s",lable[i]);
printf(" %d\n",count[i]+1);
}
}
return 0;
}
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
unordered_map<string,int> func(string str){
unordered_map<string, int> res;
string word;
int len = str.length();
for (int i = 0; i < len; i++)
{
if (str[i] == ' ')
{
if (word.length() == 0)
continue;
else if(word[0]=='#'){
res[word]++;
word = "";
}
else {
word = "";
}
}
else {
word += str[i];
}
}
return res;
}
int main()
{
string str = "i have brought #peace #peace #freedom #justice and #security to my new empire";
auto ans = func(str);
cout << ans.size() << endl;
for (auto x : ans) {
cout << x.first << " " << x.second << endl;
}
}
#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>
#include <string>
#include <vector>
int main()
{
std::vector<std::string> tokens;
std::copy_if(std::istream_iterator<std::string>(std::cin),
std::istream_iterator<std::string>(),
std::back_inserter(tokens),
[](const auto& token)
{ return token.length() > 1
&& std::count(token.begin(), token.end(), '#') == 1
&& token[0] == '#'; });
std::map<std::string, int> statistics;
std::for_each(tokens.begin(), tokens.end(),
[&statistics](const auto& token)
{ statistics[token]++; });
std::cout << statistics.size() << std::endl;
std::for_each(statistics.begin(), statistics.end(),
[](const auto& item)
{ std::cout << item.first << " " << item.second << std::endl; });
return 0;
}