字符串搜索。HOJ1530 Compound Words。

stl set實現字符串搜索。。效率一般。(附二分搜索。)

Compound Words


Time limit:1sec.Submitted:233
Memory limit:32MAccepted:81
Source: Waterloo ACM Programming Contest Sep 28, 1996

You are to find all the two-word compound words in a dictionary. A two-word compound word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.


Input

Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 120,000 words.


Output

Your output should contain all the compound words, one per line, in alphabetical order.

Sample Input

a
alien
born
less
lien
never
nevertheless
new
newborn
the
zebra
Sample Output
alien
newborn

說明:
在給出的詞中找出可有有任意另外兩個詞拼接而成的詞輸出。
用set存儲與搜索。
代碼如下:
#include<iostream>
#include
<set>
using namespace std;

int main() {
set<string> s;
string tmp;
while (cin >> tmp)
s.insert(tmp);
set<string>::iterator it = s.begin();
for (it; it != s.end(); it++) {
tmp
= *it;
for (int i = 1; i < tmp.length(); i++) {
if (s.find(tmp.substr(0, i)) != s.end() && s.find(tmp.substr(i, tmp.length() - i)) != s.end()) {
cout
<< tmp << endl;
break;
}
}
}
return 0;
}

二分實現。效率高很多。。
ContractedBlock.gifExpandedBlockStart.gifView Code
#include<stdio.h>
#include
<string.h>
char ss[120010][50];
bool Bsearch(char *s, int n);

int main() {
char s1[100], *s2;
int i = 0, j, k, n, len;
while (scanf("%s", ss[i]) != EOF) i++;
n
= i;
for (i = 0; i < n; i++) {
len
= strlen(ss[i]);
for (j = 0, k = 0; j < len - 1; j++) {
s1[k
++] = ss[i][j];
s1[k]
= '\0';
s2
= ss[i] + j + 1;
if (Bsearch(s1, n) && Bsearch(s2, n)) {
puts(ss[i]);
break;
}
}
}
return 0;
}

bool Bsearch(char *s, int n) {
int left = 0, right = n - 1, middle, r;
while (left <= right) {
middle
= (left + right) / 2;
r
= strcmp(s, ss[middle]);
if (r == 0) return true;
if (r < 0)
right
= middle - 1;
else
left
= middle + 1;
}
return false;
}

轉載于:https://www.cnblogs.com/yangchenhao8/archive/2011/06/09/2076812.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/275470.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/275470.shtml
英文地址,請注明出處:http://en.pswp.cn/news/275470.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

字節3-1前端面試官自學Vue的正確姿勢

大家好&#xff0c;我是若川。前不久和一個字節前端TL朋友聊天&#xff0c;說到大廠前端供需脫節的情況。特別是使用Vue框架的&#xff0c;因為簡單易學好上手&#xff0c;但是能夠深入理解的人并不多&#xff0c;大多都只停留在應用層面&#xff0c;缺乏更深層面的理解。尤其是…

android視圖工具,android studio的HierarchyViewer工具如何知道android屏幕的視圖屬性

讓我們首先看看adb是如何組織的.它有3個主要組件,如here所述 –> client – 在用于開發的機器上運行的客戶端.通過發出adb命令從shell調用客戶端.層次結構查看器還會創建adb客戶端.> server – 在開發計算機上作為后臺進程運行的服務器.它將從adb客戶端發出的命令傳遞給a…

云時代架構讀后感4--IT架構的本質

IT架構的本質 原文地址&#xff1a;http://mp.weixin.qq.com/s?__bizMzAwNTQ4MTQ4NQ&mid2453562304&idx1&snbe86a7bc682c4e76e06b87a10ad45188&chksm8cd136a2bba6bfb430103e50f94b670e799412d0a1cae4eded0eb901847b6d462359ae317635&mpshare1&scene23…

蘋果風格ui_蘋果如何使Soft-UI成為未來

蘋果風格ui重點 (Top highlight)Apple announced some pretty wild updates at WWDC 2020 today.蘋果今天在WWDC 2020上宣布了一些相當瘋狂的更新。 But technology aside, let’s focus on how their UI has changed. It went through the first bitmap representations, thr…

【數據結構】量子危機

問題 宇宙時間公元 5.55 億年&#xff0c;由于某種原因兩大聯盟展開了激戰&#xff08;maxingc 聯盟采用了微子技術&#xff09;&#xff1a; 邪惡的 maxingc 聯盟采集好了微子能&#xff0c;就要運輸。Maxingc 聯盟的領袖 xc 此時才發現&#xff0c;自己的軍事基地中由微子發射…

android 自定義menu背景,Android編程實現自定義系統菜單背景的方法

本文實例講述了Android編程實現自定義系統菜單背景的方法。分享給大家供大家參考&#xff0c;具體如下&#xff1a;不多說&#xff0c;上圖&#xff0c;見代碼。package lab.sodino.menutest;import android.content.Context;import android.app.Activity;import android.os.Bu…

面試官問 async、await 函數原理是在問什么?

大家好&#xff0c;我是若川。這是 源碼共讀活動《1個月&#xff0c;200人&#xff0c;一起讀了4周源碼》 第四期&#xff0c;紀年小姐姐的第四次投稿。紀年小姐姐通過本次學習提早接觸到generator&#xff0c;協程概念&#xff0c;了解了async/await函數的原理等。第四期是 學…

一步步優化JVM六:優化吞吐量[轉]

2019獨角獸企業重金招聘Python工程師標準>>> 原文&#xff1a;http://ganlv.iteye.com/blog/1571315 參考&#xff1a;http://www.myexception.cn/software-architecture-design/1455594.html 現代JVM是一個具有靈活適應各種應用能力的軟件&#xff0c;盡管很多應用…

element-ui 網格_UI備忘單:列表與網格

element-ui 網格重點 (Top highlight)Grids or lists? That is the question we will look at in this cheat sheet. While they can be used anywhere in your site, we are going to look primarily at search results, catalogs and newsfeeds. Making this choice will de…

asp.net mvc使用TagBuilder的應用程序集

在asp.net mvc編寫擴展方法中需要使用到TagBuilder類&#xff0c;根據msdn的說法應該應用System.Web.Mvc.dll 程序集。 TagBuilder 構造函數 .NET Framework 4 初始化 TagBuilder 類的新實例。命名空間&#xff1a; System.Web.Mvc程序集&#xff1a; System.Web.Mvc&#xf…

50行 koa-compose,面試常考的中間件原理原來這么簡單?

大家好&#xff0c;我是若川。源碼共讀《1個月&#xff0c;200人&#xff0c;一起讀了4周源碼》 活動第五期是學習 koa 源碼的整體架構&#xff0c;淺析koa洋蔥模型原理和co原理中的koa-compose源碼原理&#xff0c;閱讀不到50行的koa-compose源碼。這篇是izjing小哥哥的投稿。…

sqlite3源碼編譯到Android,實現SQLite跨全平臺使用

文&#xff0f;何曉杰Dev(高級Android架構師)著作權歸作者所有&#xff0c;轉載請聯系作者獲得授權。初看這個標題你可能會不解&#xff0c;SQLite 本身就是一個跨平臺的數據庫&#xff0c;在這里再說跨平臺有什么意義呢&#xff1f;其實不然&#xff0c;目前我就遇到了一個項目…

在Red Hat 4 AS U7上安裝oracle10gR2

軟件&#xff1a;Red Hat 4 AS U7, Oracle 10g R2 for linux32, VMWare 7, Windows 7詳細步驟清單&#xff1a;在Red Hat 4 AS U7上安裝oracle10gR2 1. 硬件需求&#xff1a; &#xff1d;&#xff1d;&#xff1d;&#xff1d;&#xff1d;&#xff1d;&#xff1d;&#xff1…

illustrator下載_平面設計:16個Illustrator快捷方式可加快工作流程

illustrator下載I know, I know — keyboard shortcuts sound so nerdy, and you’re a graphic designer, not an IT Director, why should you learn keyboard shortcuts?我知道&#xff0c;我知道—鍵盤快捷鍵聽起來很書呆&#xff0c;而且您是圖形設計師&#xff0c;而不是…

手把手教你五分鐘扒個源碼寫個無敵外掛

大家好&#xff0c;我是若川。源碼共讀《1個月&#xff0c;200人&#xff0c;一起讀了4周源碼》 活動進行到第五期了&#xff0c;歡迎點鏈接加我微信 ruochuan12 報名參加。前言前段時間群里分享了一個小游戲&#xff0c;多次懷疑自己的眼睛以后&#xff0c;嘗試去寫個外掛。中…

Kubernetes 1.14重磅來襲,多項關鍵特性生產可用

走過了突飛猛進的2018年&#xff0c;Kubernetes在2019年終于迎來了第一個大動作&#xff1a;Kubernetes 1.14版本的正式發布&#xff01;Kubernetes 本次發布的 1.14 版本&#xff0c;包含了 31 項增強&#xff0c;其中 10 項為 GA&#xff0c;12 項進入 beta 試用階段&#xf…

中英文

http://it.freesion.com/3220/4888028/13606306/#轉載于:https://www.cnblogs.com/yqskj/articles/2082326.html

open ai gpt_讓我們來談談將GPT-3 AI推文震撼到核心的那條推文

open ai gpt重點 (Top highlight)“設計師”插件 (The ‘Designer’ plugin) A couple days ago, a tweet shared by Jordan Singer turned the heads of thousands of designers. With the capabilities of GPT-3 (from OpenAI), he shared a sample of what he was able to c…

我歷時3年才寫了10余篇源碼文章,但收獲了100w+閱讀

你好&#xff0c;我是若川。最近來了一些讀者朋友&#xff0c;在這里簡單介紹自己的經歷&#xff0c;也許對你有些啟發。之前發過這篇文章&#xff0c;現在修改下聲明原創&#xff0c;方便保護版權。最近組織了源碼共讀活動1個月&#xff0c;200人&#xff0c;一起讀了4周源碼&…

android onlescan 參數,Android BLE:從iOS外設廣告時,在onLeScan()回調中檢索服務UUID

我正在使用Nexus 4(4.4 kitkat)作為中央和iPad作為外設.外圍設備有廣告服務.廣告包有一些數據(22字節)的服務UUID.當我嘗試從Android掃描外圍設備時,iPad外圍設備被發現.但是當我嘗試從回調中的scanRecord參數獲取服務UUID時,我找不到它.我得到的是外設發送的20byte數據.當我嘗…