Encoded Barcodes
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1530 Accepted Submission(s): 538
Problem Description
All the big malls need a powerful system for the products retrieval. Now you are employed design a sub-system: reading the barcodes and return the matching products.
A barcode is an optical machine-readable representation of data, which shows certain data on certain products. A barcode consists of a series of bars with different widths. In our system, the barcodes have been scanned and the widths have been recorded. Every consecutive eight bars are considered as representing the ASCII code of a character, each bar for each bit. Ideally, there should be only two kinds of widths in the eight bars, and the width of the wider bar is twice of the narrower. The wider bar indicates 1, while the narrower indicates 0. However, due to the inaccuracy of printing and scanning, there will be an error of at most 5%. That is, if the pretended exact width is x, you may get a value in the range [0.95x, 1.05x].
For example, the width sequence "10.0 20.0 10.0 10.0 10.0 10.0 10.0 20.0" is a valid barcode of our system, and it means (01000001) 2, which is (65) 10 and the corresponding character is "A". Note that "10.5 20.1 10.1 10.2 9.9 9.7 10.0 19.9" is also a valid barcode representing the same letter.
You are given the names of all the products and many queries. Every name contains lower-case letters only, and the length is no more than 30. The queries are represented as barcodes. For each query, you should decode it to a string S, and report the amount of products whose prefix is S. For the output may be very large, you only need to output the sum of all the queries for each case.
A barcode is an optical machine-readable representation of data, which shows certain data on certain products. A barcode consists of a series of bars with different widths. In our system, the barcodes have been scanned and the widths have been recorded. Every consecutive eight bars are considered as representing the ASCII code of a character, each bar for each bit. Ideally, there should be only two kinds of widths in the eight bars, and the width of the wider bar is twice of the narrower. The wider bar indicates 1, while the narrower indicates 0. However, due to the inaccuracy of printing and scanning, there will be an error of at most 5%. That is, if the pretended exact width is x, you may get a value in the range [0.95x, 1.05x].
For example, the width sequence "10.0 20.0 10.0 10.0 10.0 10.0 10.0 20.0" is a valid barcode of our system, and it means (01000001) 2, which is (65) 10 and the corresponding character is "A". Note that "10.5 20.1 10.1 10.2 9.9 9.7 10.0 19.9" is also a valid barcode representing the same letter.
You are given the names of all the products and many queries. Every name contains lower-case letters only, and the length is no more than 30. The queries are represented as barcodes. For each query, you should decode it to a string S, and report the amount of products whose prefix is S. For the output may be very large, you only need to output the sum of all the queries for each case.
Input
There are several test cases in the input. The first line of each case contains two integers N and M (1 <= N <= 10000, 1 <= M <= 2000), indicating the number of products and queries. Then N lines follow, indicating the names of the products. Note that the names may be duplicated. Then M query blocks follow. The first line of each query block is an integer K (0 < K <= 30) indicating the length of the query, then K lines follow, each line contains 8 positive float numbers, indicating the barcode for each character.
You can assume that the barcodes are always valid, and always represent lower-case letters.
You can assume that the barcodes are always valid, and always represent lower-case letters.
Output
Output one line for each test case, indicating the sum of all the query results as described above.
Sample Input
4 3 apple apple avatar book 1 1 2 2 1 1 1 1 2 2 1 2 2 1 1 1 1 2 10.1 20.1 19.9 20.0 10.2 9.8 9.9 10.0 1 1 2 2 1 1 1 2 2
Sample Output
5HintThere is only one test case. The first query is "a", and the answer is 3. The second query is "ap", and the answer is 2. The third query is "c", and the answer is 0. So the total sum is 3+2+0 = 5.
Source
AC自动机版改
#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
#define TRUE true
#define FALSE false
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 100005
#define cha 258
const int Root = 0;
struct Trie
{
int next[N][cha], end[N];
int Root, L;
int newnode()
{
for (int i = 0; i < cha; i++)
next[L][i] = -1;
end[L] = 0;
L++;
return L - 1;
}
void init()
{
L = 0;
Root = newnode();
}
void insert(char s[])
{
int len = strlen(s);
int now = Root;
int ti;
for (int i = 0; i < len; i++)
{
ti = s[i];
if (next[now][ti] == -1)
next[now][ti] = newnode();
now = next[now][ti];
end[now]++;
}
}
int query(int buf[],int len)
{
int now = Root;
int ti;
bool flag = false;
for (int i = 0; i < len; i++)
{
ti = buf[i];
if (next[now][ti] != -1)
now = next[now][ti];
else
return 0;
}
return end[now];
}
};
Trie ac;
int buf[1000];
char s[10000];
//a ap c
char getint(double t[], double avg)
{
int num = 0;
for (int i = 0, j = 7; i < 8; i++, j--)
{
if (sgn(t[i] * 1.05 - avg * 0.95) >= 0)
num |= (1 << j);
}
return num;
}
double tiao[100];
int main()
{
#ifdef DeBUGs
freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
int n, m;
while (scanf("%d%d", &n, &m) + 1)
{
ac.init();
for (int i = 0; i < n; i++)
{
scanf("%s", s);
ac.insert(s);
}
int k;
int bufnum = 0;
int sum = 0;
for (int i = 0; i < m; i++)
{
scanf("%d", &k);
memset(buf, 0, sizeof(buf));
bufnum = 0;
for (int j = 0; j < k; j++)
{
double avg = 0;
for (int l = 0; l < 8; l++)
{
scanf("%lf", &tiao[l]);
avg += tiao[l];
}
buf[bufnum++] = getint(tiao, avg / 8.0);
}
// printf("%s\n", buf);
sum += ac.query(buf,bufnum);
}
printf("%d\n", sum);
}
return 0;
}
用普通版ac的
#include <iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<bitset>
using namespace std;
#define zero {0}
typedef struct Trie
{
int v;
Trie *next[26];
} Trie;
char s[100100][100];
Trie root;
void createTrie(char *str)
{
int len = strlen(str);
Trie *p = &root, *q;
for (int i = 0; i < len; ++i)
{
int id = str[i] - 'a';
if (p->next[id] == NULL)
{
q = (Trie *)malloc(sizeof(root));
q->v = 1;
for (int j = 0; j < 26; ++j)
q->next[j] = NULL;
p->next[id] = q;
p = p->next[id];
}
else
{
p->next[id]->v++;
p = p->next[id];
}
}
}
int findTrie(char *str)
{
int len = strlen(str);
int ret = 0, x, y;
Trie *p = &root;
for (int i = 0; i < len; ++i)
{
int id = str[i] - 'a';
if (i != 0) x = p->v;
else x = -1;
p = p->next[id];
y = p->v;
if (i != 0 && x == y)
ret++;
}
return len - ret;
}
int findnum(char *str)
{
int len = strlen(str);
Trie *p = &root;
for (int i = 0; i < len; i++)
{
int id = str[i] - 'a';
if (p->next[id])
{
p = p->next[id];
}
else
{
return 0;
}
}
return p->v;
}
int main()
{
// freopen("C:\\Users\\Sky\\Desktop\\1.in","r",stdin);
int i, n, sum, m;
while (scanf("%d%d", &n, &m) != EOF)
{
for (i = 0; i < 26; ++i)
root.next[i] = NULL;
getchar();
for (i = 1; i <= n; i++)
{
gets(s[i]);
createTrie(s[i]);
}
sum = 0;
for (i = 0; i < m; i++)
{
int zimushu;
scanf("%d", &zimushu);
char str[1005] = zero;
double bit[10] = zero;
for (int j = 0; j < zimushu; j++)
{
double ave = 0;
bitset<32>b;
for (int k = 7; k >= 0; k--)
{
scanf("%lf", &bit[k]);
ave += bit[k];
}
ave /= 8;
for (int k = 7; k >= 0; k--)
{
if (bit[k] > ave)
{
b.set(k, 1);
}
else
{
b.set(k, 0);
}
}
str[j] = b.to_ulong();
// cout<<b.to_string()<<endl;
}
// cout<<str<<endl;
sum += findnum(str);
}
// scanf("%s",str);
// cout<<findnum(str)<<endl;
printf("%d\n", sum);
}
return 0;
}