UVA10815 Andy's First Dictionary

问题链接:UVA10815 Andy‘s First Dictionary

题意简述:输入一个文本文件,从中提取出字典,重复的单词被去掉。

这个问题用C++语言编写程序,主要是为了练习使用STL的功能。另外一点,C++编写程序效率会更高。

使用STL容器类的set,可以方便地去重复,而且还会自动排序。

程序中,使用C语言的库函数strtok()来切割单词,并且用空格‘ ‘作为分隔符。这是一种简便的做法。

另外一种切割字符串的方法是,使用STL的字符串流(sstream)实现。

AC的C++程序如下:

/* UVA10815 Andy's First Dictionary */

#include <iostream>
#include <cstring>
#include <set>

using namespace std;

#define MAXN 512

set<string> dict;

int main()
{
    char s[MAXN], delim[] = " ", *p;

    while(cin >> s) {
        p = s;
        while(*p) {
            if(isalpha(*p))
                *p = tolower(*p);
            else
                *p= ' ';
            p++;
        }

        p = strtok(s, delim);
        while(p) {
             dict.insert(p);
             p = strtok(NULL, delim);
        }
    }

    for(set<string>::iterator iter =dict.begin(); iter != dict.end(); iter++)
        cout << *iter << "\n";

    return 0;
}

UVA10815 Andy's First Dictionary

时间: 2024-08-19 06:17:51

UVA10815 Andy's First Dictionary的相关文章

UVa10815 Andy&#39;s First Dictionary (STL)

链接:http://acm.hust.edu.cn/vjudge/problem/18649分析:set容器应用.set中每个元素最多只出现一次.自定义类型也可以构造set,必须定义“小于”运算符.set中的元素从小到大已排好序. 1 #include <iostream> 2 #include <string> 3 #include <cctype> 4 #include <set> 5 #include <sstream> 6 using n

UVa10815,Andy&#39;s First Dictionary, set,stringstream

题意: 输入一个文本,找出所有不同的单词,按字典序输出所有单词. stringstream通常是用来做数据转换的 相比c库的转换,它更加安全,自动和直接 #include <sstream> stringstream stream stream <<value   输入 stream >>value   读取 #include <cstdio> #include <set> #include <string> #include <

安迪的第一个字典 (Andy&#39;s First Dictionary,UVa10815)

题目描述: #include<iostream> #include<string> #include<set> #include<sstream> using namespace std; set<string> dic; string s, buf; int main() { while(cin >> s) { for(int i = 0; i < s.length(); i++) if(isalpha(s[i])) s[i]

【UVA - 10815】Andy&#39;s First Dictionary (set)

Andy's First Dictionary Description 不提英文了 直接上中文大意吧 XY学长刚刚立下了再不过CET就直播xx的flag,为了不真的开启直播模式,XY学长决定好好学习英语.于是他每天都读一篇只包含生词的英语文章,并以自己高达450的智商在一秒钟之内记忆下来. 现在给你一篇XY学长今天要读的文章,请你写一个程序,输出他都学习到了哪些单词.要求:如果文章中有相同的单词,那么仅仅输出一次:而且如果两个单词只有大小写不同,将他们视为相同的单词. Input 测试数据将输入

UVA - 10815 Andy&#39;s First Dictionary

1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <set> 5 using namespace std; 6 7 set<string> out; 8 9 int main() 10 { 11 string s,temp; 12 while(cin>>s) 13 { 14 int len(s.size()); 15 for(int

Winter-2-STL-E Andy&#39;s First Dictionary 解题报告及测试数据

use stringstream Time Limit:3000MS     Memory Limit:0KB Description Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thin

Andy&#39;s First Dictionary

Description Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thinking up all the words himself, he has a briliant idea. F

UVa - 10815 Andy&#39;s First Dictionary(STL)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18649 #include <iostream> #include <string> #include <set> #include <sstream> using namespace std; /******************************************************************

UVA 10815 Andy&#39;s First Dictionary(字符处理)

Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thinking up all the words himself, he has a briliant idea. From his book