YTU 2774: Prepare for CET6
2774: Prepare for CET6
时间限制: 1 Sec 内存限制: 128 MB提交: 40 解决: 37
题目描述
Hard to force the CET4&6 is imminent, which makes most of the students a headache. This does not, Liu is trying his best to prepare for the CET6, trying to do a variety of previous year’s problem. But the most helpless for Liu is a quick read of this topic, so he thought of a thing does not make sense is that the statistical article the total number in different words. Here your task is to help Liu solve this problem.
输入
Multiple sets of data, each row is an article. Each article is consisted by lowercase letters and spaces, no punctuation, encounter # when the input end.
输出
Each group output only an integer, which alone make the trip, the integer representing the total number of different words in an article.
样例输入
you are my friend
can you can a can as a canner can can a can
#
样例输出
4
5
提示
c++ stl集合(Set)是一种包含已排序对象的关联容器。set会根据待定的排序准则,自动将元素排序,不允许元素重复。
1) 不能直接改变元素值,因为那样会打乱原本正确的顺序,要改变元素值必须先删除旧元素,则插入新元素
2) 不提供直接存取元素的任何操作函数,只能通过迭代器进行间接存取,而且从迭代器角度来看,元素值是常数
3) 元素比较动作只能用于型别相同的容器(即元素和排序准则必须相同)
set模板原型://Key为元素(键值)类型
template <class Key, class Compare=less<Key>, class Alloc=STL_DEFAULT_ALLOCATOR(Key) >
添加元素
set<string> set1; //empty set
set1.insert("the"); //set1 now has one element
set1.insert("and"); //set1 now has two elements
c++ stl容器set成员函数:
size()--集合中元素的数目
C++引入了ostringstream、istringstream、stringstream这三个类,要使用他们创建对象就必须包含<sstream>这个头文件。
istringstream类用于执行C++风格的串流的输入操作。
ostringstream类用于执行C风格的串流的输出操作。
strstream类同时可以支持C风格的串流的输入输出操作。
istringstream的构造函数原形如下:
istringstream::istringstream(string str);
它的作用是从string对象str中读取字符。
#include<iostream>
#include<sstream>
using namespace std;
int main()
{
string str, line;
while(getline(cin, line))
{
istringstream stream(line);
while(stream>>str)
cout<<str.c_str()<<endl;
}
return 0;
}
测试:
Input:
Abdc Xcs Xa Xa
Output:
Abdc
Xcs
Xa
你 离 开 了 , 我 的 世 界 里 只 剩 下 雨 。 。 。
#include <iostream> #include <string.h> #include <stdio.h> #include <algorithm> using namespace std; int main() { string s; char t; while(getline(cin,s)&&s!="#") { string p[1000]; int len=s.length(),i=0,count=0,k=0,con; while(i<len) { while(s[i]!=' '&&i<len)t=s[i++],p[count]+=t; ++count,i++; } sort(p,p+count); con=count; for(i=1; i<count; ++i) { k=0; while(p[i]==p[i-1])k++,i++; con-=k; } cout<<con<<'\12'; } return 0; }