您的位置:首页 > 其它

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写的话会方便很多,我呢就纯当容器的运用题好了。
本题目还有一些别的写法,比如哈希函数,动态字典树。当然我不晓得。
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva map