HDU 1247 Hat's Words (map+string)
2015-08-26 16:14
381 查看
Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10993 Accepted Submission(s): 3944[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
Your output should contain all the hat’s words, one per line, in alphabetical order.
[align=left]Sample Input[/align]
a
ahat
hat
hatword
hziee
word
[align=left]Sample Output[/align]
ahat
hatword
这道题小弱初看时, 好像没有什么思路。然而其实计算机的最大好处就在计算的快速。 所以直接暴力就行啦! 现在一看到题老是想是否要用到什么高级的结构,什么技巧, 其实有些题, 废话少说,直接上暴力就行啦。
#include<iostream> #include<cstdio> #include<string> #include<map> using namespace std; map<string,int> M; string str[50005]; int main() { //freopen("in.txt", "r", stdin); int k = -1; while(cin>>str[++k]) M[str[k]] = 1; for(int i=0; i<=k; i++) { int len= str[i].size()-1; for(int j=1; j<len; j++) { string s1(str[i], 0, j); string s2(str[i], j); if(M[s1]==1&&M[s2]==1) { cout<<str[i]<<endl; break; } } } return 0; }
本题技巧: string s1(str[i], 0, j);把str[i]中的0~j的字符赋给s1. string s2(str[i], j)把str[i]中的j~末尾的字符赋给s2.
相关文章推荐
- 启动php-fpm时报错
- spring+Quartz定时器
- FastDfs+nginx环境配置
- 代理使用一(cell页面的跳转)
- opencv矩阵元素类型
- POJ 2287 && HDU 1052 Tian Ji -- The Horse Racing(贪心)
- 7-使用MATLAB进行编程
- Java多线程 -- 线程的休眠
- java迭代器
- Android UI设计:Gallery
- 单列模式例子
- Ubuntu 12.04 安装 gcc-4.8 及使用
- Qt把double类型的时间转化为QDateTime类型
- 算法系列-交换两个变量
- ASP如何获取真实IP地址
- 片段缓存
- BLToolkit Output Parameter in DataAccessor
- 2054 A == B ?【字符串】
- Java发送邮件
- 图结构练习——BFSDFS——判断可达性