您的位置:首页 > 其它

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

2015-12-06 18:09 696 查看
//自己不会写 但是看懂了学长写的

#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啦

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: