http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Contest/contestproblem/cid/2009/pid/2828
sdut 2828 字典树模板题目 静态的空间
#include <iostream>
#include <cstdio>
#include <string.h>
#include <cstdlib>
#include <iostream>
using namespace std;
struct node
{
int v;
node *next[26];
node()
{
v = 0;
for(int i = 0;i < 26;i++){
next[i] = NULL;
}
}
}T[1000000];
int top;
void creattrie(node *&root, char s[])
{
node *p = root, *q;
for(int i = 0;s[i];i++){
int id = s[i]-'a';
if(p->next[id] == NULL){
q = &T[top++];
p->next[id] = q;
}
p = p->next[id];
}
p->v++;
}
int findtree(node *root, char s[])
{
node *p = root;
for(int i = 0;s[i];i++){
int id = s[i]-'a';
if(p->next[id]==NULL){
return 0;
}
p = p->next[id];
}
if(p->v){
return 1;
}else {
return 0;
}
}
int main()
{
int n, m;
char s[20];
node *root;
while(~scanf("%d %d", &n, &m)){
memset(T,0,sizeof(T));
top = 0;
if(n==0&&m==0) break;
root = &T[top++];
for(int i = 0;i < n;i++){
scanf("%s", s);
creattrie(root,s);
}
while(m--){
scanf("%s", s);
int t = findtree(root,s);
if(t){
printf("Yes\n");
}else {
printf("No\n");
}
}
}
return 0;
}
http://acm.hdu.edu.cn/showproblem.php?pid=1251
hdu1251 用new过 不能申请静态的空间 否则gg
#include <iostream>
#include <cstdio>
#include <string.h>
#include <cstdlib>
#include <algorithm>
using namespace std;
struct node
{
int v;
node *next[26];
node(){
v = 0;
for(int i = 0;i < 26;i++){
next[i] = NULL;
}
}
};
int top;
void creat(node *&root, char s[])
{
node *p = root, *q;
for(int i = 0;s[i];i++){
int id = s[i]-'a';
if(p->next[id]==NULL){
q = new node;
q->v++;
p->next[id] = q;
}else{
p->next[id]->v++;
}
p = p->next[id];
}
}
int findtree(node *root, char s[])
{
node *p = root;
for(int i = 0;s[i];i++){
int id = s[i]-'a';
if(p->next[id]==NULL){
return 0;
}
p = p->next[id];
}
return p->v;
}
int main()
{
node *root = new node;
char s[15];
while(gets(s)){
if(!strcmp(s,"")) break;
creat(root,s);
}
while(scanf("%s", s)!=EOF){
printf("%d\n", findtree(root,s));
}
return 0;
}
sdut 3039 看好题目要求就行跟hdu1251差不多
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string.h>
#include <algorithm>
using namespace std;
struct node
{
int v;
node *next[10];
node()
{
v = 0;
for(int i = 0;i < 10;i++){
next[i] = NULL;
}
}
}T[100010];
int top;
void fan(char s[])
{
char d[10];
strcpy(d,s);
int len = strlen(d);
int i;
for(i=0;d[i];i++){
s[i] = d[len-1-i];
}
s[i] = '\0';
}
void creat(node *&root, char s[])
{
node *p = root, *q;
for(int i = 0;s[i];i++){
int id = s[i]-'0';
if(p->next[id]==NULL){
q = &T[top++];
q->v++;
p->next[id] = q;
}else{
p->next[id]->v++;
}
p = p->next[id];
}
p->v--;
}
int findtree(node *root, char s[])
{
node *p = root;
for(int i = 0;s[i];i++){
int id = s[i]-'0';
if(p->next[id]==NULL){
return 0;
}
p = p->next[id];
}
return p->v;
}
int main()
{
int n;
char s[10];
while(~scanf("%d", &n)){
memset(T,0,sizeof(T));
top = 0;
node *root = &T[top++];
for(int i = 0;i < n;i++){
scanf("%s", s);
fan(s);
creat(root,s);
}
int m;scanf("%d", &m);
for(int i = 0;i < m;i++){
scanf("%s", s);
fan(s);
printf("%d\n", findtree(root,s));
}
}
return 0;
}
hdu 2072一开始的前导的空格没有处理wa了
#include <iostream>
#include <cstdio>
#include <string.h>
#include <cstdlib>
#include <algorithm>
using namespace std;
struct node
{
int v;
node *next[26];
node(){
v =0;
for(int i = 0;i < 26;i++){
next[i] = NULL;
}
}
};
int sum;
void creat(node *&root, char s[])
{
node *p =root, *q;
for(int i = 0;s[i];i++){
int id = s[i] - 'a';
if(p->next[id]==NULL){
q = new node;
p->next[id]=q;
}
p = p->next[id];
}
if(p->v==0){
sum++;
}p->v++;
}
int main()
{
char s[100000];
char s1[1005];
while(gets(s)&&s[0]!='#'){
sum = 0;
node *root = new node;
int i = 0;
int l = strlen(s);
while(1){
while(s[i++]==' ');i--;
if(i==l) break;
int top = -1;
int j;
for(j = i;s[j]!=' '&&s[j]!='\0';j++){
s1[++top] = s[j];
}
s1[++top] = '\0';
creat(root,s1);
if(s[j]=='\0') break;
else {
while(s[j]==' '){
j++;
}
}
i = j;
}
printf("%d\n", sum);
}
return 0;
}