uva10391(复合词)

第一次做是用暴力的方法一个一个找,意料之中超市了;

然后就想把每个单词拆分,虽然时间复杂度也是平方级别,但是单词不会太长,循环次数小很多,试过之后果然AC了!

 1 #include <iostream>
 2 #include <queue>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cstdio>
 6 #include <set>
 7 #include <string>
 8 #include <sstream>
 9 #include <vector>
10
11 using namespace std;
12 set<string> words;
13 set<string> answers;
14 int main()
15 {
16     string s,buf;
17     int i;
18     while(cin >> s)
19         words.insert(s);
20     set<string>::iterator it;
21     for(it = words.begin();it != words.end();it++)
22     {
23         s = *it;
24         for(i=0;i<s.length()-1;i++)
25         {
26             string a = s.substr(0,i+1);
27             string b = s.substr(i+1,s.length()-i-1);
28             if(words.find(a)!=words.end() && words.find(b)!=words.end())
29                 answers.insert(s);
30         }
31     }
32     for(it = answers.begin();it!= answers.end();it++)
33         cout << *it << "\n";
34     return 0;
35 }
时间: 2024-11-04 09:35:21

uva10391(复合词)的相关文章

UVA-10391(字符串检索)

题意: 给定一个字典,要求找出所有的复合词; 思路: 用map把词都存起来,再把词拆开看是否是出现过的单词; AC代码: #include <bits/stdc++.h> #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; #define For(i,j,n

uva 10391复合词compound words(Trie+set)

给定一个词典,要求求出其中所有的复合词,即恰好有两个单词连接而成的词 trie存储以该单词为前缀的单词数量,然后对于每个单词,看在字典中的以该单词为前缀的单词"减去"原单词剩下的单词是否在字典中,如果是储存这个答案到ans的set中 #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include&l

复合词 (Compund Word,UVa 10391)

题目描述: 题目思路: 用map保存所有单词赋键值1,拆分单词,用map检查是否都为1,即为复合词 1 #include <iostream> 2 #include <string> 3 #include <map> 4 using namespace std; 5 map<string,int> dict ; 6 string str[120005] ; 7 int main(int argc, char *argv[]) 8 { 9 int count

ACM复合词

10391 Compound Words 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 numb

UVa 10391 Compound Words(复合词)

题意  输出所有输入单词中可以由另两个单词的组成的词 STL set的应用  枚举每个单词的所有可能拆分情况  看拆开的两个单词是否都存在  都存在的就可以输出了 #include <bits/stdc++.h> using namespace std; string a, b; set<string> s; set<string>::iterator i; int main() { int l; while(cin >> a) s.insert(a); f

uva10391

//两重for遍历tle //a alien born less lien never nevertheless new newborn the zebra //60ms #include <iostream>#include <set>#include <string>using namespace std;set<string> arr, ans;int main(){ string t; while (cin >> t)  arr.inse

不定代词some、any以及some、any、no与-one、-body、-thing的复合词

本文摘自复旦大学出版社<中学英语语法(高中第二版)>,作者魏孟勋 1.some,someone,somebody,something一般用于肯定句.例如:Some said yes, and some said no. I didn't know whom to believe.I saw somebody upstairs, but I couldn't see him clearly.He whispered something in my ear, and I couldn't help

重读规则

一般来讲,英语单词的重读是没有固定的规律而言的.但有些规则在多数情况下是可以适用的,这些规则有:重读规律1.一般2.3个音节的单词,重读在第一音节上.如:'apple 'carry 'problem. 但如果这个单词是带有前缀的,则重读则在前缀后.如:a'bove a'go com'bine en'courage2.3个或以上音节的单词,重读在倒数第三个音节上. 'family de'mocracy phy'losophy 3.带有后缀的单词,重读在后缀前(以下后缀除外:-ed,-es,-er,

第五章-習題(1-11)待續

5-1 代碼對齊(UVa 1593) 不難,按行讀取,然後stringstream輸入到vector<string>那裏去,算出行最大單詞數,再算出列單詞最大寬度,然後就可以格式化輸出了: #include<iostream> #include<string> #include<algorithm> #include<vector> #include<cstdio> #include<sstream> using name