Link:
http://acm.hdu.edu.cn/showproblem.php?pid=1247
The main effect of the topic:
By giving multiple words in a dictionary, you can see that some of the words are made up of the other two words in the dictionary. Output all words that match this condition in order.
Analysis and Summary:
Use the dictionary tree to save all the words, and then for all the words one by one enumerations, for each word, and then "split", split may have many situations, so enumerate the midpoint of the word split, in the dictionary order to find the split between the existence of two parts, the existence is output.
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <cctype> using Namespa
CE std;
const int MAXN = 2000000;
const int KIND = 26;
struct node{bool Isword;
Node *next[kind];
void Init () {isword=false;
memset (Next, 0, sizeof (next));
}}HEAP[MAXN];
int cntnode,pos;
Char word[50005][50];
Inline node* NewNode () {heap[cntnode].init ();
Return &HEAP[cntNode++];
} void Insert (node *root, char *str) {int cnt=0;
for (char *p=str; *p; ++p) {int ch=*p-' a ';
if (root->next[ch]==null) {root->next[ch]=newnode ();
root = root->next[ch];
} Root->isword = true;
BOOL Search (node *root, char *str) {for (char *p=str; *p; ++p) {int ch=*p-' a ';
if (root->next[ch]==null) return false;
root=root->next[ch]; } RETurn root->isword;
int main () {cntnode=0; pos=0;
Node *root=newnode ();
Char str[100];
while (gets (Word[pos])) {Insert (root, word[pos++]);
for (int i=0; i<pos; ++i) {if (word[i][0]==0 | | strlen (word[i)) ==1) continue;
memset (str, 0, sizeof (str));
for (int j=0; J<strlen (Word[i))-1; ++j) {str[j]=word[i][j];
if (search (ROOT,STR) && search (root,word[i]+j+1)) {puts (word[i]);
Break
}} return 0; }
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/