安迪的第一個字典(Andy's First Dictionary,UVa 10815)

来源:http://www.cnblogs.com/yjlblog/archive/2017/06/06/6947747.html
-Advertisement-
Play Games

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 ...


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. From his bookshelf he would pick one of his favourite story books, from which he would copy out all the distinct words. By arranging the words in alphabetical order, he is done! Of course, it is a really time-consuming job, and this is where a computer program is helpful.

 

You are asked to write a program that lists all the different words in the input text. In this problem, a word is defined as a consecutive sequence of alphabets, in upper and/or lower case. Words with only one letter are also to be considered. Furthermore, your program must be CaSe InSeNsItIvE. For example, words like "Apple", "apple" or "APPLE" must be considered the same.

Input

The input file is a text with no more than 5000 lines. An input line has at most 200 characters. Input is terminated by EOF.

 

Output

Your output should give a list of different words that appears in the input text, one in a line. The words should all be in lower case, sorted in alphabetical order. You can be sure that he number of distinct words in the text does not exceed 5000.

 

Sample Input

Adventures in Disneyland

Two blondes were going to Disneyland when they came to a fork in the
road. The sign read: "Disneyland Left."

So they went home.

 

Sample Output

a
adventures
blondes
came
disneyland
fork
going
home
in
left
read
road
sign
so
the
they
to
two
went
were
when
1.中文翻譯

 

 

大體的中文意思就是:輸入一個文本,找出所有不同的單詞(連續的字母序列),按字典序列從小到大輸出。單詞不區分大小寫,輸入輸出和上面一樣。

2.分析

 

 

這個題比較簡單,我學習的是《演算法競賽入門經典》這本書的代碼。

1.學會運用set容器;(有個小知識點,set容器,元素只能出現一次,並且插入可以從小到大排序)

2.學習字元函數庫中常用的函數

3.學會stringstream(可參考這篇博文:http://blog.csdn.net/xw20084898/article/details/21939811)

4.最後運行記得是  在空行  ctrl+z +回車。(至於為什麼,參考博文:http://blog.csdn.net/kevin_ut/article/details/8576740)

3.說明

 

 

set是一個集合 和康托前輩的一樣 集合中的元素不重覆 且集合中的元素是有序的(自動有序化) TY菌介紹說其內部實質是一個平衡樹

set不是數組 只能通過迭代器(iterator)把裡面的元素倒出來 迭代器相當於是指針 掃描的是地址 因此輸出的時候需要用*variation

4.聲明

 

 

需要用到頭文件set

set<string> dict; 建立一個集合 其名為dict 基類型是string

 

5.常用函數

 

 

 

dict.begin() dict.end()返回集合最初和最後的元素的地址

 

這是寫這個例題用到的

6.代碼

 

 

 1 #include<iostream>  
 2 #include<string>  
 3 #include<set>  
 4 #include<sstream>  
 5 using namespace std;  
 6   
 7 set<string> dict;//set up a set called dict-short for dictionary,and it's based on string type;  
 8   
 9 int main(){  
10     string s,buf;  
11     while (cin>>s){  
12         for (int i=0;i<s.length();i++)  
13           if (isalpha(s[i])) s[i]=tolower(s[i]);//if it's an letter,turn it lowercase.Others turn space and use stringstream to leave it out=ignore it  
14           else s[i]=' ';  
15         stringstream ss(s);  
16         while (ss>>buf) dict.insert(buf);//insert it into the set which is already in order,TYkon said it's a balanced tree inside  
17     }  
18     for (set<string>::iterator it=dict.begin();it!=dict.end();++it)//iterator just like a point,scan it from beginning to end and output  
19       cout<<*it<<endl;//NOTICE output by point  
20     return 0;  
21 }

還是英文註釋不過看懂應該問題不大

isalpha()判斷是否是字母 如果是就用頭tolower()轉換成小寫 否則就轉為空格 這樣是為了字元串流重讀入的時候能夠把空格去掉

特別強調一下本例中出現的迭代器

1 for (set<string>::iterator it=dict.begin();it!=dict.end();++it)//iterator just like a point,scan it from beginning to end and output  
2       cout<<*it<<endl;//NOTICE output by point

迭代器iterator相當於是個類型 it是一個變數 基類型是iterator 它從開始掃到結尾的前一個【據TY菌解釋 像數組一樣 最後一位是沒有值的 數組是一個結束標誌 不知集合最後是什麼】輸出的時候 由於it掃描的是地址 要輸出*it

這個好像用到了stl set的語法....

 


顯然對於set我還知之甚少 還需要在今後的學習中不斷掌握

今天晚上此時已是0:15,寫完博客,明天上課.....

 

 

本文部分引用http://blog.csdn.net/ametake

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 聲明:本文為轉載內容,感謝原作者辛勤勞動。原鏈接為:http://www.cnblogs.com/chinahbzm/articles/1423875.html 1)建立空連接: net use \\IP\ipc$ "" /user:"" (一定要註意:這一行命令中包含了3個空格) 2)建立非空連接 ...
  • 引言 最新有一個winform項目使用的是DevExpress的控制項,所以最近都在摸索使用這套控制項,實在是佩服整套控制項的強大,同時代碼寫起來也簡潔。客戶有一個需求,希望報表結果能在外接的大屏幕上定時滾動。這個報表我們使用的控制項就是GridControl,查詢結果一屏不能顯示完全,增加一個定時器,指定 ...
  • 什麼是AspectCore Project ? "AspectCore Project" 是適用於Asp.Net Core 平臺的輕量級 Aop(Aspect oriented programming) 解決方案,它更好的遵循Asp.Net Core的模塊化開發理念,使用AspectCore可以更容 ...
  • 一:安裝 運行項目後,進去安裝頁面,按照提示輸入,填寫的管理員郵箱和密碼,即超管的賬號,登陸後,頂部會有後臺鏈接。 二:無法進入後臺 解決辦法:生成下解決方案就好了。 三:首頁Banner在哪改? 進入後臺,Configuration -> Widgets -> Widgets.NivoSlider ...
  • IdentityServer4 預設提供了兩種證書加密配置: 這兩種證書加密方式,都是臨時使用,每次重啟項目的時候,都會重新生成一個新的證書,這時候就會導致一個問題,重啟之前生成的 ,在重啟之後,就不適用了,因為證書改變了,對應的加密方式也改變了,所以,就會出現下麵這個問題: 錯誤信息: Www A ...
  • 前言 樹莓派(Raspberry Pi,RPi)是專門為學生電腦編程教育而設計,只有信用卡大小的卡片式電腦,可以運行Linux或者Windows 10 IoT Core操作系統。本文將利用樹莓派和UWP(Universal Windows Platform)開發技術搭建一個可以實時監控的氣象站系統 ...
  • foreach可以遍歷一些數據類型。由圖可知,被遍歷的數據類型必須擁有GetEnumerator的公共方法。 在使用foreach遍歷時涉及到var推斷類型,這時如何不是實現泛型版本,則一律是object類型。 foreach遍歷只能讀取數據,不能修改數據,可以通過string檢測,string是可 ...
  • ​上節我們提到了正則表達式,它提升了文本處理的表達能力,本節就來討論正則表達式,它是什麼?有什麼用?各種特殊字元都是什麼含義?如何用Java藉助正則表達式處理文本?都有哪些常用正則表達式?由於內容較多,我們分為三節進行探討,本節先簡要探討正則表達式的語法。 正則表達式是一串字元,它描述了一個文本模式 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...