码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1251 统计难题

时间:2017-04-01 09:25:30      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:string   for   nod   ace   技术分享   pid   char   std   代码   

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1251

技术分享

技术分享

解题思路:

一个简单的字典树问题。

实现代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

const int N=27;

struct node{
    int cnt;
    node* childs[N];
    node(){
        cnt=0;
        for(int i=0;i<N;i++)
            childs[i]=NULL;
    }
};

node *root=new node();
node *current,*newnode;

void insert(char *str){
    current=root;
    for(int i=0;i<strlen(str);i++){
        int m=str[i]-a;
        if(current->childs[m]!=NULL){
            current=current->childs[m];
            ++(current->cnt);
        }else{
            newnode=new node;
            ++(newnode->cnt);
            current->childs[m]=newnode;
            current=newnode;
        }
    }
}

int search(char *str){
    current=root;
    for(int i=0;i<strlen(str);i++){
        int m=str[i]-a;
        if(current->childs[m]==NULL)
            return 0;
        current=current->childs[m];
    }
    return current->cnt;
}

int main()  
{  
    char str[20];  
    while(gets(str),strcmp(str,""))  
    insert(str);  
    while(gets(str)!=NULL)  
    printf("%d\n",search(str));  
    return 0;  
}  

 

HDU 1251 统计难题

标签:string   for   nod   ace   技术分享   pid   char   std   代码   

原文地址:http://www.cnblogs.com/IKnowYou0/p/6654472.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!