亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

HDOJ 1247 HDU 1247 Hat’s Words ACM 1247 IN

系統(tǒng) 2599 0

MiYu原創(chuàng), 轉(zhuǎn)帖請注明 : 轉(zhuǎn)載自? ______________白白の屋 ?? ??

?

題目地址 :

?? ? http://acm.hdu.edu.cn/showproblem.php?pid=1247?

題目描述:

Hat’s Words

Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1384????Accepted Submission(s): 506


Problem Description
A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.
You are to find all the hat’s words in a dictionary.
?

Input
Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 50,000 words.
Only one case.
?

Output
Your output should contain all the hat’s words, one per line, in alphabetical order.
?

Sample Input
              
a ahat hat hatword hziee word
?

Sample Output
              
ahat hatword
?

題目分析 :

??

?? ? ? ?字典樹的題目, 這個題的方法比較暴力, ?在輸入的時候?qū)⒚總€單詞都加入字典樹, ?并用數(shù)組將所有的串都存起來來.

在輸入完成后, ?對每個單詞進行拆分, 也就是暴力枚舉單詞不同位置的前后部分, 看在字典樹中是否存在, 存在即輸出.

不過貌似數(shù)據(jù)比較弱, 說是按字典順序輸出, 但其實他的輸入本就是按字典順序輸入的, 所以將排序也面了 , 呵呵.

?? ?另外,其實這題用STL 做更簡單, 當然追求效率除外.

trie 代碼:

?/*

MiYu原創(chuàng), 轉(zhuǎn)帖請注明 : 轉(zhuǎn)載自 ______________白白の屋

?? ? ? ? ?http://www.cnblog.com/MiYu

Author By : MiYu

Test ? ? ?: 1

Program ? : 1247

*/


#include <iostream>

#include <algorithm>

#include <string>

using namespace std;

typedef struct dictor DIC;

DIC *root = NULL;

struct dictor {

?? ? ? dictor (){ exist = false; memset ( child , 0 , sizeof ( child ) ); }

?? ? ? void insert ( char *ins );

?? ? ? bool find ( const char *ins );

private:

?? ? ? DIC *child[26];

?? ? ? bool exist;?

};

void dictor::insert ( char *ins )

{

?? ? ? ? ? ?DIC *cur = root,*now;

?? ? ? ? ? ?int len = strlen ( ins );

?? ? ? ? ? ?for ( int i = 0; i < len; ++ i )

?? ? ? ? ? ?{

?? ? ? ? ? ? ? ? ?if ( cur->child[ ins[i] - 'a' ] != NULL )?

?? ? ? ? ? ? ? ? ?{

?? ? ? ? ? ? ? ? ? ? ? cur = cur->child[ ins[i] - 'a' ];

?? ? ? ? ? ? ? ? ?}

?? ? ? ? ? ? ? ? ?else

?? ? ? ? ? ? ? ? ?{

?? ? ? ? ? ? ? ? ? ? ? now = new DIC;

?? ? ? ? ? ? ? ? ? ? ? cur->child[ ins[i] - 'a' ] = now;

?? ? ? ? ? ? ? ? ? ? ? cur = now;

?? ? ? ? ? ? ? ? ?}

?? ? ? ? ? ?}?

?? ? ? ? ? ?cur->exist = true;

}

bool dictor::find ( const char *ins )

{

?? ? ? ? ? ?DIC *cur = root;

?? ? ? ? ? ?int len = strlen ( ins );

?? ? ? ? ? ?for ( int i = 0; i < len; ++ i )

?? ? ? ? ? ?{

?? ? ? ? ? ? ? ? if ( cur->child[ ins[i] - 'a' ] != NULL )

?? ? ? ? ? ? ? ? ? ? ?cur = cur->child[ ins[i] - 'a' ]; ?

?? ? ? ? ? ? ? ? else

?? ? ? ? ? ? ? ? ? ? ?return false;?

?? ? ? ? ? ?}?

?? ? ? ? ? ?return cur->exist;

}

char words[50050][100];

char s1[100],s2[100];

DIC dict;

int main ()

{

?? ?root = &dict;

?? ?int n = 0;

?? ?while ( scanf ( "%s",words[n] ) != EOF )

?? ?{

?? ? ? ? ? ?dict.insert ( words[n++] );

?? ?}

?? ?for ( int i = 0; i < n; ++ i )

?? ?{

?? ? ? ? ?int len = strlen ( words[i] );

?? ? ? ? ?for ( int j = 1; j < len; ++ j )

?? ? ? ? ?{

?? ? ? ? ? ? ? ?memset ( s1, 0, sizeof ( s1 ) );

?? ? ? ? ? ? ? ?memset ( s2, 0, sizeof ( s2 ) );

?? ? ? ? ? ? ? ?strncpy ( s1, words[i], j );

?? ? ? ? ? ? ? ?strcpy ( s2, words[i]+j );

?? ? ? ? ? ? ? ?if ( dict.find ( s1 ) && dict.find ( s2 ) )

?? ? ? ? ? ? ? ?{

?? ? ? ? ? ? ? ? ? ? printf ( "%s\n", words[i] );

?? ? ? ? ? ? ? ? ? ? break;?

?? ? ? ? ? ? ? ?}

?? ? ? ? ?} ?

?? ?} ?

?? ?//system ( "pause" );

?? ?return 0;

}


STL ?代碼 :

?

?/*

MiYu原創(chuàng), 轉(zhuǎn)帖請注明 : 轉(zhuǎn)載自 ______________白白の屋

?? ? ? ? ?http://www.cnblog.com/MiYu

Author By : MiYu

Test ? ? ?: 2

Program ? : 1247

*/


#include <iostream>

#include <string>

#include <map>

using namespace std;

map < string , int > mp;

string str[50005];

int main ()

{

?? ?int n = 0;

?? ?while ( cin >> str[n] ) mp[ str[n++] ] = 1;

?? ?for ( int i = 0; i < n; ++ i )

?? ?{

?? ? ? ? ?unsigned len = str[i].size ();

?? ? ? ? ?for ( unsigned j = 1; j < len; ++ j )

?? ? ? ? ?{

?? ? ? ? ? ? ? string s1 ( str[i], 0, j );

?? ? ? ? ? ? ? string s2 ( str[i], j );

?? ? ? ? ? ? ? if ( mp[s1] == 1 && mp[s2] == 1 )

?? ? ? ? ? ? ? {

?? ? ? ? ? ? ? ? ? ?cout << str[i] << endl;?

?? ? ? ? ? ? ? ? ? ?break;

?? ? ? ? ? ? ? }

?? ? ? ? ?}?

?? ?}

?? ?return 0;

}


?

?

HDOJ 1247 HDU 1247 Hat’s Words ACM 1247 IN HDU


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 免费一级黄色片 | 黄色录像一级毛片 | 一区二区在线视频 | h片免费网站 | 在线国产播放 | 一级a毛片 | 六月丁香婷婷激情国产 | 玖玖在线播放 | 久久人人爽人人爽人人片av不 | 亚洲欧洲日产国码久在线观看 | 欧美爱爱爱爱免费视频 | 色网站欧美 | 亚洲综合网站 | 久久中文字幕久久久久91 | 豆国产96在线 | 亚洲 | 国内亚州视频在线观看 | 亚洲综合中文 | 免费福利在线 | 夜夜天天干 | 日本激情视频一区二区三区 | 普通话对白国产情侣自啪 | 国产激情视频趣趣在线观看的 | 亚洲精品亚洲人成在线播放 | 久久精品无码一区二区日韩av | 日韩黄色网址 | 亚洲精品国产一区二区三区四区 | 欧美亚洲高清日韩成人 | 国内拍拍自拍视频在线观看 | 国产在线精品一区二区 | 夜夜摸视频网 | 91久久精品午夜一区二区 | 97影院97伦里片 | 女人18一级特级毛片免费看 | 欧美男女啪啪 | 国产成人精品日本亚洲语音2 | 欧美午夜伦y4480私人影院 | 波多野结衣久久高清免费 | 国产精品视频2021 | 久久精品亚洲热综合一本奇米 | 国产1区二区| 蜜桃久久久久久久久久久 |