字串處理: 猴子打字

來源:互聯網
上載者:User

浙大動物園最近僱傭了很多程式猿,動物園的管理者希望將這些猿猴們培養成未來的諾貝爾獎、圖靈獎和菲爾茲獎獲得者。為此,動物園針對猿猴們展開了一項專項訓練:撰寫文章。

在最近的一次練習中,這些猿猴們在鍵盤上胡亂地敲出了一些長度均不超過 5000 的字串,其中的每個字元都是從空格、半形標點符號、數字、英文大小寫等 95 個可列印字元中等機率隨機敲出的。

管理員認為,如果在字串中出現了 “Nature & Science” 這個子串(不含引號,大小寫敏感),則猿猴們已經達到了訓練目標,他們會將首個 “Nature & Science” 之前的全部內容當作文章發表出去,供世人瞻仰。 輸入格式

一個整數 T,表示有多少組測試資料。

接下來的 T 行,每行有一個字串。注意字串內可能含有空格,因此建議使用 getchar() 或其他類似的方式進行讀入。 輸出格式

對於每組測試資料,如果猴子們能夠發表文章,則輸出一行字串,表示文章中的內容,否則什麼也不做。 範例輸入

4)#>alV.RIqq;ik3&[<>$Hello, Nature & Science: WorldNature & Science: Hello, WorldThe Cake is a Lie. Nature & Science.
範例輸出
Hello, The Cake is a Lie. 
提示

第一組資料中沒有出現 “Nature & Science”,所以沒有任何輸出。

剩下幾組資料中出現了 “Nature & Science”,但第三組資料的 “Nature & Science” 之前沒有字元,因此輸出了一個空行。

另外,為了說明輸出方式,部分範例資料是由動物園管理員構造的,但我們保證實際測試資料全部都是猿猴們撰寫出來的文章,嚴格符合題目描述中所保證的條件。


#include <iostream>#include <stdio.h>#include <string>using namespace std;int main(){int T;string str;cin >> T;getchar();while (T--){getline(cin, str);int pos = str.find("Nature & Science");if (pos >= 0){for (int i = 0; i < pos; i++){printf("%c", str[i]);}printf("\n");}}return 0;}




#include <stdio.h>#include <iostream>#include <string>using namespace std;int main() {int T;string str;scanf("%d", &T);getchar();while (T--){getline(cin, str);int pos = str.find("Nature & Science");if (pos == 0) printf("\n");if (pos > 0 && pos < 5010){str = str.substr(0, pos);printf("%s\n", str.c_str());//cout << str << endl;}}return 0;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.