CPE 一顆星
題目連結:https://zerojudge.tw/ShowProblem?problemid=c114
內容
給定多個關鍵字以及若干字串,求出包含最多關鍵字的字串。
每筆測資第一列第一個數字 K (1 <= K <= 20) 代表關鍵字數量,第二個數字 E (1 <= E <= 20) 代表字串數量。接著為關鍵字和字串。
範例輸入
5 3
dog
ate
homework
canary
died
My dog ate my homework.
Can you believe my dog died after eating my canary… AND MY HOMEWORK?
This excuse is so good that it contain 0 keywords.
6 5
superhighway
crazy
thermonuclear
bedroom
war
building
I am having a superhighway built in my bedroom.
I am actually crazy.
1234567890…..,,,,,0987654321?????!!!!!!
There was a thermonuclear war!
I ate my dog, my canary, and my homework … note outdated keywords?
範例輸出
Excuse Set #1
Can you believe my dog died after eating my canary… AND MY HOMEWORK?
Excuse Set #2
I am having a superhighway built in my bedroom.
There was a thermonuclear war!
想法
先將字串中非英文字母的字元轉成空白,英文字母則轉成小寫,利用 stringstream 將字串內的單字一個一個抓出來跟關鍵字比對,並記錄次數,最後輸出最多關鍵字的字串即可。
程式碼
1 |
|