You are to find all the two-word compound words in a dictionary. A two-word compound word is a word

本文详细介绍了如何使用C++的string类和map数据结构实现高效字符串匹配,通过构建哈希映射来快速查找包含特定子串的单词,并输出匹配成功的单词。代码实例展示了算法实现过程,包括单词输入、哈希映射初始化、子串匹配与输出等关键步骤。
摘要由CSDN通过智能技术生成
//自己不会写 但是看懂了学长写的
#include <iostream>
#include <string> //string类
#include <map>
using namespace std;
int main()
{
    map<string,bool>hash;//hash散列 我发现把hash改成别的也对
    string str[150000];
    hash.clear(); //清空hash
    string temp;
    string a,b;
    int num=0;
    while(cin>>str[num])  //输入str
    {
        hash[str[num]]=true;
        num++;
    }
   // num 输入的单词个数
    for(int i=0;i<num;i++)
    {
        temp=str[i];  //temp 为第一个 第二个 第三个 ......单词
        for(int j=0;j<temp.size()-1;j++)//加1 是为了防止越界
        {
            a=temp.substr(0,j+1); //a 是第一个 第二个 第三个 ......单词从第一个字母到第j+1个字母
            b=temp.substr(j+1);   //b 是第一个 第二个 第三个 ......单词从第j+1个字母直到最后
            if(hash[a]&&hash[b])    //如果 输入的单词中有 a 又有 b
            {
                cout<<temp<<endl;    //那么输出temp
                break;
            }
        }
    }
    return 0; //hahaha ok啦
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值