Hdu 1075 What Are You Talking About
2012-09-11 21:47
381 查看
字典树,处理数据较麻烦。
CODE:
#include <iostream>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
struct Trie
{
Trie *next[26];
char save[12];
int value;
Trie()
{
for(int i = 0; i < 26; i++)
next[i] = 0;
value = 0;
}
};
void insert(Trie *&root, char *sz1, char *sz2)
{
int i = 0, branch = 0;
Trie *p = root;
if(!p)
{
p = new Trie();
root = p;
}
while(sz2[i])
{
branch = sz2[i]-'a';
if(!p->next[branch]) p->next[branch] = new Trie();
p = p->next[branch];
i++;
}
p->value = 1;
strcpy(p->save, sz1);
}
int find(Trie *root, char *sz1)
{
int i = 0, branch = 0;
Trie *p = root;
if(!p) return -1;
while(sz1[i])
{
branch = sz1[i]-'a';
if(!p->next[branch]) return 0;
if(sz1[i+1] == '\0')
{
if(p->next[branch]->value)
{
printf("%s", p->next[branch]->save);
return 1;
}
return 0;
}
p = p->next[branch];
i++;
}
}
int main()
{
char sz1[12], sz2[12], c;
char temp[12];
scanf("%s", temp);
Trie *root = NULL;
while(scanf("%s", sz1))
{
if(!strcmp(sz1, "END")) break;
scanf("%s", sz2);
insert(root, sz1, sz2);
}
scanf("%s", temp);
getchar();
int cnt = 0;
while(c = getchar())
{
if(isalpha(c)) sz1[cnt++] = c;
else
{
sz1[cnt] = '\0';
if(!strcmp(sz1, "END")) break;
if(!find(root, sz1))
printf("%s", sz1);
printf("%c", c);
cnt = 0;
}
}
}
CODE:
#include <iostream>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
struct Trie
{
Trie *next[26];
char save[12];
int value;
Trie()
{
for(int i = 0; i < 26; i++)
next[i] = 0;
value = 0;
}
};
void insert(Trie *&root, char *sz1, char *sz2)
{
int i = 0, branch = 0;
Trie *p = root;
if(!p)
{
p = new Trie();
root = p;
}
while(sz2[i])
{
branch = sz2[i]-'a';
if(!p->next[branch]) p->next[branch] = new Trie();
p = p->next[branch];
i++;
}
p->value = 1;
strcpy(p->save, sz1);
}
int find(Trie *root, char *sz1)
{
int i = 0, branch = 0;
Trie *p = root;
if(!p) return -1;
while(sz1[i])
{
branch = sz1[i]-'a';
if(!p->next[branch]) return 0;
if(sz1[i+1] == '\0')
{
if(p->next[branch]->value)
{
printf("%s", p->next[branch]->save);
return 1;
}
return 0;
}
p = p->next[branch];
i++;
}
}
int main()
{
char sz1[12], sz2[12], c;
char temp[12];
scanf("%s", temp);
Trie *root = NULL;
while(scanf("%s", sz1))
{
if(!strcmp(sz1, "END")) break;
scanf("%s", sz2);
insert(root, sz1, sz2);
}
scanf("%s", temp);
getchar();
int cnt = 0;
while(c = getchar())
{
if(isalpha(c)) sz1[cnt++] = c;
else
{
sz1[cnt] = '\0';
if(!strcmp(sz1, "END")) break;
if(!find(root, sz1))
printf("%s", sz1);
printf("%c", c);
cnt = 0;
}
}
}
相关文章推荐
- HDU- 1075 What Are You Talking About (Trie or map)
- HDU 1075 What Are You Talking About
- hdu 1075 What Are You Talking About
- hdu 1075 What Are You Talking About(HashMap水过,可用字典树)
- HDU 1075 - What Are You Talking About
- hdu 1075 What Are You Talking About(字典树)
- HDU 1075 What Are You Talking About // 字符串映射,Trie
- hdu 1075 What Are You Talking About
- hdu 1075 What Are You Talking About
- HDU 1075 What Are You Talking About
- hdu 1075 What Are You Talking About(map)
- HDU 1075-What Are You Talking About(Trie)
- hdu 1075 What Are You Talking About
- hdu 1075 What Are You Talking About
- HDU - 1075 What Are You Talking About(字典树)
- hdu 1075 What Are You Talking About
- hdu 1075 What Are You Talking About(Trie树)
- HDU 1075 What Are You Talking About
- HDU1075——What Are You Talking About
- hdu 1075 What Are You Talking About