hdu 2072 单词数

来源:互联网 发布:手机导航网站源码 编辑:程序博客网 时间:2024/05/17 04:27

hdu 2072 单词数

题目描叙:给你 一串字符串,该字符串里只含有 小写字母和空格,求该字符串中 不同 的单词数。

解题思路:单纯天真的我,被WA成了 煞笔。于是去搜解题报告 。最主要还是题目没看清,没看见不同那两个字。果然还是太傻。 当完完全全看懂题目意思后,就没有思路了,去搜结题报告。 选了一个短小精悍的代码,研究了半天,有点眉目了。

#include<iostream>#include<cstdio>#include<set>#include<string>#include<cstring>using namespace std;int main(){    set<string>word;    string s="";//字符串变量的值是空    char c;    while(scanf("%c",&c)&&c!='#')    {        if(c==' ')        {            if(s.length())                word.insert(s);// 把 s 插入到 word中              s.clear(); // 清空 s         }        else if(c=='\n')        {            if(s.length())                word.insert(s);            printf("%d\n",word.size());            word.clear();            s.clear();        }        else        {            s+=c;        }    }    return 0;}//  set 这个容器的最大好就是它里面的元素没有重复的 

以上代码要对 set 和 string 的特性特别熟悉才能写出。

给一个关于set 的 博客:http://blog.sina.com.cn/s/blog_779cf3410101389s.html

string的 用法:http://jingyan.baidu.com/article/20b68a8854f919796dec6265.html

刚八德!

0 0
原创粉丝点击