HDU - 3724 Encoded Barcodes (字典树)

题意:给定n个字符串和m个经过处理得到的字符串,问对于m个字符串中的每个字符串,n个字符串中以该字符串为前缀的个数。
分析:
1、误差在[0.95x, 1.05x],因此求8个数的平均数,大于平均数为1,否则为0。
2、字典树求前缀个数。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
    if(fabs(a - b) < eps) return 0;
    return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 10;
const double pi = acos(-1.0);
const int MAXN = 40 + 10;
const int MAXT = 10000 + 10;
using namespace std;
double tmp[10];
int POW[10];
void init(){
    int tmp = 1;
    for(int i = 0; i < 8; ++i){
        POW[i] = tmp;
        tmp *= 2;
    }
}
struct Tree{
    int value;
    Tree *_next[26];
};
Tree root;
void Build(char *s){
    int len = strlen(s);
    Tree *p = &root;
    Tree *q;
    for(int i = 0; i < len; ++i){
        int id = s[i] - 'a';
        if(p -> _next[id] == NULL){
            q = (Tree*)malloc(sizeof root);
            q -> value = 1;
            for(int j = 0; j < 26; ++j){
                q -> _next[j] = NULL;
            }
            p -> _next[id] = q;
            p = p -> _next[id];
        }
        else{
            p = p -> _next[id];
            ++p -> value;
        }

    }
}
int Find(string t){
    int len = t.size();
    Tree *p = &root;
    for(int i = 0; i < len; ++i){
        int id = t[i] - 'a';
        if(p -> _next[id] == NULL) return 0;
        p = p -> _next[id];
    }
    return p -> value;
}
int main(){
    int N, M;
    init();
    while(scanf("%d%d", &N, &M) == 2){
        for(int i = 0; i < 26; ++i){
            root. _next[i] = NULL;
        }
        char s[35];
        for(int i = 0; i < N; ++i){
            scanf("%s", s);
            Build(s);
        }
        int ans = 0;
        while(M--){
            int K;
            scanf("%d", &K);
            string t;
            while(K--){
                double sum = 0;
                for(int i = 0; i < 8; ++i){
                    scanf("%lf", &tmp[i]);
                    sum += tmp[i];
                }
                double ave = sum / 8;
                int tot = 0;
                for(int i = 0; i < 8; ++i){
                    if(tmp[i] > ave) tot += POW[7 - i];
                }
                t += 'a' + tot - 97;
            }
            ans += Find(t);
        }
        printf("%d\n", ans);
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/tyty-Somnuspoppy/p/7265707.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值