UVa 10391(Compound Words)

#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[n])
    {
        Map[str[n]]=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写的话会方便很多,我呢就纯当容器的运用题好了。
本题目还有一些别的写法,比如哈希函数,动态字典树。当然我不晓得。    
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值