标签:ase string out sam new lower cat alpha end
You are to find all the two-word compound words in a dictionary. A two-word compound word is a
word in the dictionary that is the concatenation of exactly two other words in the dictionary.
Input
Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will
be no more than 120,000 words.
Output
Your output should contain all the compound words, one per line, in alphabetical order.
Sample Input
a
alien
born
less
lien
never
nevertheless
new
newborn
the
zebra
Sample Output
alien
newborn
题意分析:给定一系列单词(总共不超过120000个单词),找出某些单词,它们是由其他两个单词组合而成的,按照字典序排列。因为所有的单词已经按照字典序排好了,因此从前往后逐个判断即可,因为单词的长度没有给出来,但估计最大长度len不会太大记为常量,将单词拆分成两个单词,查找是否存在于列表中即可。查找直接通过map(或二分查找),总计复杂度约O(N*logN*LEN*LEN)
AC源码:
#include <stdio.h> #include <string> #include <map> using namespace std; int main() { // freopen("d:\\data1.in","r",stdin); map<string, int> mp; char s[100]; while(scanf("%s", s)!=EOF) { mp[s] = 1; } map<string, int>::iterator it; for(it=mp.begin();it!=mp.end();it++) { for(int i=1;i<it->first.length()-1;i++) { string ss1 = it->first.substr(0, i); string ss2 = it->first.substr(i, it->first.length()-i); if(mp.find(ss1)!=mp.end() && mp.find(ss2)!=mp.end()) { printf("%s\n", it->first.c_str()); break; } } } return 0; }
紫书第五章训练 uva 10391 Compound Words by crq
标签:ase string out sam new lower cat alpha end
原文地址:http://www.cnblogs.com/tzcacm/p/6846225.html