现在的位置: 首页 > 综合 > 正文

hdu 1251 统计难题

2018年01月12日 ⁄ 综合 ⁄ 共 1487字 ⁄ 字号 评论关闭

只能默默的说,指针什么的对于我这个没学过c语言的弱菜真的很................................

大二好好学c++~~~

#include <set>
#include <map>
#include <ctime>
#include <queue>
#include <cmath>
#include <stack>
#include <limits>
#include <vector>
#include <bitset>
#include <string>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <string.h>
#include <iostream>
#include <algorithm>
#define LL long long
#define Vi vector<int>
#define Si set<int>
#define readf freopen("input.txt","r",stdin)
#define writef freopen("output.txt","w",stdout)
#define FF(i,a) for(int i(0); i < (a); i++)
#define FD(i,a) for(int i(a); i >= (1); i--)
#define FOR(i,a,b) for(int i(a);i <= (b); i++)
#define FOD(i,a,b) for(int i(a);i >= (b); i--)
#define PD(a) printf("%d\n",a)
#define SET(a,b) memset(a,b,sizeof(a))
#define SD(a) scanf("%d",&(a))
#define LN printf("\n")
#define PS printf(" ")
#define pb push_back
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const double pi = acos(-1.0);
const int maxn = 301;
const int INF = ~0U>>1;
const int dx[]={0,1,0,-1};
const int dy[]={1,0,-1,0};
using namespace std;
class tire{
    public:
    int pre;
    bool isword;
    tire *next[26];
    tire(){
        SET(next,0);
        pre=0;
        isword=false;
    }
}root;
void insert(char *word){
    tire *p=&root;
    int k=0;
    while(word[k]!='\0'){
        if(!p->next[word[k]-'a'])   p->next[word[k]-'a']=new tire;
        p=p->next[word[k]-'a'];
        p->pre++;
        k++;
    }
    p->isword=true;
}
int query(char *word){
    tire *p=&root;
    int k=0;
    while(word[k]!='\0' && p->next[word[k]-'a']){
        p=p->next[word[k]-'a'];
        k++;
    }
    if(word[k]=='\0') return p->pre;
    return 0;
}
int main()
{
    char dir[15],ss[15];
    while(gets(dir)&& dir[0]!='\0'){
        insert(dir);
    }
    while(~scanf("%s",ss)){
        printf("%d\n",query(ss));
    }
    return 0;
}

【上篇】
【下篇】

抱歉!评论已关闭.