UVA-10391-复合词

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39651984/article/details/79041831

AC

【分析】使用set<string>,然后对每个单词的所有左右切分方案进行遍历,判断切分出来的两个字符串两边是否同时存在与set中,符合条件的直接输出即可,这里set中的字符串已经按照默认字典序排序。

陈峰大哥的代码

#include <bits/stdc++.h>
using namespace std;


#define _for(i,a,b) for(int i = (a); i < (b); ++i)
const int maxn = 120000 + 11;

int main(int argc, char const *argv[])
{
	set<string> words;
	string word, left, right;
	while(cin >> word) words.insert(word);

	for(const auto& s: words){
		int len = s.size();
		_for(j,1,len){
			left.assign(s,0,j);
			if(words.count(left)){
				right.assign(s,j,len-j);
				if(words.count(right)) { cout<< s << endl; break;}
			}
		}
	}
	
	return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_39651984/article/details/79041831