Exercise 5_5 uva0391 compound words (saw)

Too much food, but also want to think for a long time ...
The main sticking point is 120,000 words
if each word violence to go first in front of the word whether it is a prefix, and then determine whether there suffix, o (n * n ) algorithm ...
lexicographical sort this problem is to remind you that for the (n logn) algorithm, and half can! So how half of it, the clever use of hidden bug!
The word itself length does not exceed 100! So violent enumerate its prefixes and suffixes, and inquiry, what the query (the n-nlog)? The SET!
The ok, the Code

#include <bits/stdc++.h>
using namespace std;
set<string> dic;
int main()
{
	string s;
	while(cin >> s)
	{
		dic.insert(s);
	}
	for (set<string>:: iterator it = dic.begin(); it != dic.end(); it++)
	{
		string ss = *it,l,r;
		int len = ss.size();
		for (int i = 0; i < len; i++)
		{
			l = ss.substr(0,i+1);
			r = ss.substr(i+1,len);
			if(dic.count(l) && dic.count(r))
			{
				cout<<ss<<endl;
				break;
			}
		} 
	}
	return 0;
} 
Published 55 original articles · won praise 1 · views 2657

Guess you like

Origin blog.csdn.net/qq_37548017/article/details/100067487