首页 > 代码库 > HDU 3724 Encoded Barcodes (Trie)

HDU 3724 Encoded Barcodes (Trie)

题意:给n个字符串,给m个询问,每个询问给k个条形码。每个条形码由8个小码组成,每个小码有相应的宽度,已知一个条形码的宽度只有2种,宽的表示1,窄的表示0。并且宽的宽度是窄的宽度的2倍。由于扫描的时候有误差,每个小码的宽度为一个浮点型数据,保证每个数据的误差在5%内。所以一个条形码可以对应一个ASCC码,表示一个小写字母。k个条形码表示一个字符串s,每个询问表示给定的m个字符串中以s为前缀的字符串个数。

析:很容易看起来是Tire树, 不知道能不能暴过去,我觉得差不多可以,主要是在处理浮点数上,这个很简单,既然误差这么小,那么我们就可以取最大值和最小值然后除2,小的为0, 大的为1。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 30 * 10000 + 10;
const int mod = 10;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
    return r > 0 && r <= n && c > 0 && c <= m;
}

struct Trie{
  int ch[maxn][26];
  int val[maxn];
  int sz;
  void init(){
    memset(ch[0], 0, sizeof ch[0]);
    sz = 1;
  }

  int idx(char c){ return c - ‘a‘; }

  void insert(char *s){
    int u = 0;
    for(int i = 0; s[i]; ++i){
      int c = idx(s[i]);
      if(!ch[u][c]){
        memset(ch[sz], 0, sizeof ch[sz]);
        val[sz] = 0;
        ch[u][c] = sz++;
      }
      u = ch[u][c];
      ++val[u];
    }
  }

  int query(char *s){
    int u = 0;
    for(int i = 0; s[i]; ++i){
      int c = idx(s[i]);
      if(!ch[u][c])  return 0;
      u = ch[u][c];
    }
    return val[u];
  }
};

char s[50];

Trie trie;
double a[10];

int main(){
  while(scanf("%d %d", &n, &m) == 2){
    trie.init();
    for(int i = 0; i < n; ++i){
      scanf("%s", s);
      trie.insert(s);
    }
    LL ans = 0;
    while(m--){
      int k;
      scanf("%d", &k);
      for(int i = 0; i < k; ++i){
        double mmin = inf, mmax = -1;
        for(int j = 0; j < 8; ++j){
          scanf("%lf", a+j);
          mmin = min(mmin, a[j]);
          mmax = max(mmax, a[j]);
        }
        double ave = (mmin + mmax) / 2.0;
        int t = 0;
        for(int j = 0; j < 8; ++j)
          if(a[j] < ave)  t = t * 2;
          else t = t * 2 + 1;
        s[i] = t;
      }
      s[k] = 0;
      ans += trie.query(s);
    }
    printf("%I64d\n", ans);
  }
  return 0;
}

  

HDU 3724 Encoded Barcodes (Trie)