题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075
map可以过。。。我上的字典树,小bug有点尴尬,题目没有明确给出数据范围也是无奈。
贡献了几次RE 一次WA。尴尬。discuss里面有个说注意前缀的到是给了点tip。总体来说不错
代码:
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <functional> 3 #include <algorithm> 4 #include <iostream> 5 #include <cstring> 6 #include <cassert> 7 #include <cstdio> 8 #include <cctype> 9 #include <vector> 10 #include <string> 11 #include <queue> 12 #include <stack> 13 #include <cmath> 14 #include <map> 15 #include <set> 16 using namespace std; 17 #define rep(i,a,n) for (int i=a;i<n;i++) 18 #define per(i,a,n) for (int i=n-1;i>=a;i--) 19 #define pb push_back 20 #define mp make_pair 21 #define all(x) (x).begin(),(x).end() 22 #define fi first 23 #define se second 24 #define SZ(x) ((int)(x).size()) 25 typedef vector<int> VI; 26 typedef long long ll; 27 typedef pair<int, int> PII; 28 const ll mod = 1000000007; 29 ll powmod(ll a, ll b) { ll res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1)res = res*a%mod; a = a*a%mod; }return res; } 30 // head 31 const int inf = 0x3f3f3f3f; 32 #define maxn 15 33 #define maxnode 26 34 int trie[500000][maxnode]; 35 int val[500000][maxnode]; 36 int totid = 1; 37 38 void init(){ 39 memset(trie, 0, sizeof(trie)); 40 memset(val, -1, sizeof(val)); 41 totid = 1; 42 } 43 void insert_node(char *x, int y){ 44 int p = 1, len = strlen(x); 45 for(int i = 0; i < len - 1; i++){ 46 if(trie[p][x[i] - 'a']){ 47 p = trie[p][x[i] - 'a']; 48 } 49 else{ 50 p = trie[p][x[i] - 'a'] = ++totid; 51 } 52 } 53 val[p][x[len - 1] - 'a'] = y; 54 return; 55 } 56 int find_node(char *x){ 57 int p = 1, len = strlen(x); 58 int tmval = -1; 59 for(int i = 0; i < len; i++){ 60 if(i == len - 1){ 61 tmval = val[p][x[i] - 'a']; 62 return tmval; 63 } 64 if(trie[p][x[i] - 'a']){ 65 p = trie[p][x[i] - 'a']; 66 } 67 else{ 68 return tmval; 69 } 70 } 71 return tmval; 72 } 73 74 char dict[500005][maxn]; 75 char book[500005], chn[maxn],eng[maxn]; 76 77 int main(){ 78 scanf("%s", chn); 79 int ttid = 0; 80 init(); 81 while(scanf(" %s", chn) && strcmp(chn, "END") != 0){ 82 strcpy(dict[ttid], chn); 83 scanf(" %s",eng); 84 insert_node(eng, ttid++); 85 } 86 scanf("%s", chn); 87 getchar(); 88 while(gets(book) && strcmp(book, "END") != 0){ 89 int len = strlen(book); 90 for(int i = 0; i < len;){ 91 while(!isalpha(book[i]) && book[i]) { 92 printf("%c", book[i]); 93 i++; 94 } 95 int j = i; 96 while(isalpha(book[j])) j++; 97 char tmx[maxn]; 98 strncpy(tmx, book + i, j - i); 99 tmx[j - i] = '