UVa 10391(Compound Words)
2016-07-15 09:45
288 查看
#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <iostream> #include <algorithm> #include <cctype> #include <string> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <sstream> #define For(i,a) for(int i=0;i<a;++i) #define MAX(x,y) ((x)>(y)?(x):(y)) #define MIN(x,y) ((x)<(y)?(x):(y)) #define ALL(x) x.begin(),x.end() #define INS(x) inserter(x,x.begin()) #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <iostream> #include <algorithm> #include <cctype> #include <string> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <sstream> #define For(i,a) for(int i=0;i<a;++i) #define MAX(x,y) ((x)>(y)?(x):(y)) #define MIN(x,y) ((x)<(y)?(x):(y)) #define ALL(x) x.begin(),x.end() #define INS(x) inserter(x,x.begin()) using namespace std; typedef long long ll ; typedef unsigned long long ull ; typedef pair<int,int> point ; const int maxn=120000; map<string,int>Map; char str[maxn+10][30]; char str1[30],str2[30]; int main() { int n=0; Map.clear(); while(cin>>str ) { Map[str ]=1; n++; } for(int i=0;i<n;++i) { int len=strlen(str[i]); for(int j=0;j<len;++j) { memset(str1,0,sizeof(str1)); memset(str2,0,sizeof(str2)); strncpy(str1,str[i],j); strncpy(str2,str[i]+j,len-j); if(Map[str1] && Map[str2]) { cout<<str[i]<<endl; break; } } } return 0; } /* 这道题目刚开始的思路是双重循环暴力破解,然后稍微看了一下复杂度。12000*12000这个复杂度十成是过不了的。 果断用map: 思路呢,大概就是用map<string,int>这样的形式对每个字符串赋予一个int类型的值,这里不需要多想,赋一个1就可以了,代表存在。 然后判断的时候将每个字符串从头到尾分割成两份慢慢来判断,之后的双重循环的意义也就在于此。 将一个字符串分割成两份 strncpy(temp1, str[i], j); 这个其实就是前半段字符串 strncpy(temp2, str[i]+j, len-j); 这个呢就是后半段字符串,str[i]+j也就是从str字符串的第j个字符开始截取len-j个长度的字符赋给temp2 之后呢就是map的妙用了,一个字符串映射一个数字,这样就可以直接判断分割的字符串是否是输入时候的字符串就好了。 接下来的这组样例: qw er qwer qwerty ty qwerty这个字符串也是算复合词的,复合词里面的一段还是复合词可以合理存在. 本题目如果会用map写的话会方便很多,我呢就纯当容器的运用题好了。 本题目还有一些别的写法,比如哈希函数,动态字典树。当然我不晓得。 */
相关文章推荐
- android Google Map获取地理位置信息的方法
- Spark RDD API详解(一) Map和Reduce
- Python中map()函数浅析
- Android使用Google Map浅谈
- Erlang中的映射组Map详细介绍
- c++中map的基本用法和嵌套用法实例分析
- 百度地图给map添加右键菜单(判断是否为marker)
- JavaScript中实现Map的示例代码
- jquery中map函数与each函数的区别实例介绍
- jquery中map函数遍历数组用法实例
- jquery与google map api结合使用 控件,监听器
- 浅谈jquery的map()和each()方法
- Jquery中map函数的用法
- JavaScript中的数组遍历forEach()与map()方法以及兼容写法介绍
- JS模拟的Map类实现方法
- jquery遍历数组与筛选数组的方法
- JavaScript中实现map功能代码分享
- 用json方式实现在 js 中建立一个map
- Android Map新用法:MapFragment应用介绍
- js Map List 遍历使用示例