原题是给出一个词典,找出所有的复合词,即恰好有两个单词连接而成的单词,按照字典序输出。使用的用法是set和substr这两个办法。
#include<iostream>
#include<set>
#include<string>
#include<sstream>
using namespace std;
const int maxs = 100;
set<string> voca;
set<string> outvoca;
string sts[maxs];
void main()
{
string op, buf;
int count = 0;
getline(cin, op);
stringstream ss(op);
while(ss >> buf)
{
voca.insert(buf);
sts[count++] = buf;
}
for(int i = 0; i < count; i++)
{
bool is_find = false;
int length = sts[i].length();
for(int j = 1; j < length - 1; j++)
{
string str = sts[i].substr(0, j);
string str2 = sts[i].substr(j, length-j);
if(voca.count(str) && voca.count(str2))
{
outvoca.insert(sts[i]);
is_find = true;
break;
}
}
}
for(set<string>::iterator it = outvoca.begin(); it != outvoca.end(); ++it)
cout << *it << endl;
}