Description
利用串操作实现基于给定英文段落(100~200000)构造英文词典(英文单词按字典序排序)
Input
一篇文章(包含若干个单词,标点符号全部是标准的英文字符)。
Output
输出文章里面所有单词(按照字典序,每个单词一行)。
Sample Input
Hello world!
Sample Output
hello
world
HINT
考察知识点:串和大量单词数据的存储排序问题, 时间复杂度O(nlogn)或O(n^2),空间复杂度O(n)
Append Code
析:这个题就把一些符号都处理了,然后再把单词加入set中,然后输出就好,注意这个题输出全是小写,第一次没注意。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <list> #define ALL(x) x.begin(),x.end() #define INS(x) inserter(x,x.begin()) #define frer freopen("in.txt", "r", stdin) #define frew freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 1e5 + 5; const int mod = 1e9 + 7; const char *mark = "+-*"; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline int Min(int a, int b){ return a < b ? a : b; } inline int Max(int a, int b){ return a > b ? a : b; } inline LL Min(LL a, LL b){ return a < b ? a : b; } inline LL Max(LL a, LL b){ return a > b ? a : b; } inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } set<string> sets; set<string> :: iterator it; char s[200005]; int main(){ while(gets(s) != NULL){ int x = 0, i; for(i = 0; s[i]; ++i){ if(!isalpha(s[i])){ s[i] = 0; if(i != x) sets.insert(s+x); x = i+1; } else s[i] = towlower(s[i]); } if(i != x) sets.insert(s+x); } for(it = sets.begin(); it != sets.end(); ++it) printf("%s\n", it->c_str()); return 0; }