首页 > 代码库 > hdu 1247 Hat’s Words(map)
hdu 1247 Hat’s Words(map)
Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8527 Accepted Submission(s): 3069
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.
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.
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
Author
戴帽子的
Recommend
Ignatius.L | We have carefully selected several similar problems for you: 1251 1075 1671 1298 1800
题 意:找出一个单词,前半部是一个出现过的单词,后半部也是,记住,要严格满足这个条件
所以,其实也就是先查找一个单词的是否有前缀,再用这个单词除去前缀的部分查找是否存在一个这样的单词
CODE:
#include <iostream> #include <string> #include <map> using namespace std; map <string, int> mp; string str[50005]; int main() { int n = 0, j, len; while ( cin >> str[n] ) //输入所有的单词 mp[str[n++]] = 1; //构成映射 for ( int i = 0; i < n; ++i ) { len = str[i].size(); for ( j = 1; j < len; ++j ) { string s1 ( str[i], 0, j ); //分割单词 string s2 ( str[i], j, len - j ); if ( mp[s1] == 1 && mp[s2] == 1 ) //在单词中查找存在 { cout << str[i] << endl; //输出该单词 break; } } } return 0; }
hdu 1247 Hat’s Words(map)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。