2017暑假训练之字典树、AC自动机

HDUOJ 1251 统计难题
动态字典树写法
struct Node {
	int cnt;
	Node *next[26];
	Node() {
		cnt = 0;
		for (int i = 0; i < 26; ++i) next[i] = NULL;
	}
};

void add(char *s, Node *root) {
	Node *cur = root;
	while (*s) {
		int id = *s - 'a';
		if (!cur->next[id]) cur->next[id] = new Node;
		cur = cur->next[id];
		cur->cnt++;
		++s;
	}
}

int query(char *s, Node *root) {
	Node *cur = root;
	while (*s) {
		int id = *s - 'a';
		if (!cur->next[id]) return 0;
		cur = cur->next[id];
		++s;
	}
	return cur->cnt;
}

char s[15];

int main() {
	Node *root = new Node;
	while (gets_s(s, 15) && strcmp(s, "")) add(s, root);
	while(gets_s(s, 15) && strcmp(s, "")) printf("%d\n", query(s, root));
	return 0;
}
HDUOJ 1671 Phone List
静态字典树写法
char s[maxn][12];
int len[maxn], idx[maxn], total;
struct Node {
	int end;
	Node *next[10];
	Node() {
		end = 0;
		for (int i = 0; i < 10; ++i) next[i] = NULL;
	}
}node[maxn];

Node *_new() {
	Node *ret = &node[total];
	total++;
	return ret;
}

bool add(char *s, Node *root) {
	Node *cur = root;
	while (*s) {
		int id = *s - '0';
		if (!cur->next[id]) cur->next[id] = _new();
		cur = cur->next[id];
		if (cur->end) return false;
		++s;
	}
	cur->end = 1;
	return true;
}

void init() {
	CLEAR(node, 0);
	total = 0;
}

bool cmp(int a, int b) { return len[a] < len[b]; }

int main() {
	int T;
	scanf("%d", &T);
	while (T--) {
		init();
		int n;
		scanf("%d", &n);
		for (int i = 0; i < n; ++i)
			scanf("%s", s[i]), len[i] = strlen(s[i]), idx[i] = i;
		sort(idx, idx + n, cmp);
		Node *root = _new();
		bool ok = true;
		for (int i = 0; i < n; ++i)
			if (!add(s[idx[i]], root)) {
				ok = false;
				break;
			}
		puts(ok ? "YES" : "NO");
	}
	return 0;
}
字典树在亦或最值方面的应用
HDUOJ 4825 Xor Sum
动态trie做法 546ms
struct Node {
	Node *next[2];
	Node() { next[0] = next[1] = NULL; }
};

void add(LL x, Node *root) {
	Node *cur = root;
	for (int i = 32; i >= 0; --i) {
		int id = x >> i & 1;
		if (!cur->next[id]) cur->next[id] = new Node;
		cur = cur->next[id];
	}
}

LL query(LL x, Node *root) {
	Node *cur = root;
	LL ret = 0;
	for (int i = 32; i >= 0; --i) {
		int id = (x >> i & 1) ? 0 : 1;
		if (!cur->next[id]) id = !id;
		cur = cur->next[id];
		if(id) ret |= 1 << i;
	}
	return ret;
}

int main() {
	int T, kase = 1;
	scanf("%d", &T);
	while (T--) {
		int n, q;
		scanf("%d%d", &n, &q);
		LL x;
		Node *root = new Node;
		while (n--) scanf("%lld", &x), add(x, root);
		printf("Case #%d:\n", kase++);
		while (q--) scanf("%lld", &x), printf("%lld\n", query(x, root));
	}
	return 0;
}
静态trie做法 452ms,也没快多少。。也许我写的太渣了吧。。。
struct Node {
	Node *next[2];
	Node() { next[0] = next[1] = NULL; }
}node[maxn*40];
int total;

void init() { total = 0; CLEAR(node, 0); }

void add(LL x, Node *root) {
	Node *cur = root;
	for (int i = 32; i >= 0; --i) {
		int id = x >> i & 1;
		if (!cur->next[id]) cur->next[id] = &node[total++];
		cur = cur->next[id];
	}
}

LL query(LL x, Node *root) {
	Node *cur = root;
	LL ret = 0;
	for (int i = 32; i >= 0; --i) {
		int id = x >> i & 1 ? 0 : 1;
		if (!cur->next[id]) id = !id;
		cur = cur->next[id];
		if (id) ret |= 1 << i;
	}
	return ret;
}

int main() {
	int T, kase = 1;
	scanf("%d", &T);
	while (T--) {
		init();
		int n, q;
		LL x;
		scanf("%d%d", &n, &q);
		Node *root = &node[total++];
		while (n--) scanf("%lld", &x), add(x, root);
		printf("Case #%d:\n", kase++);
		while (q--) scanf("%lld", &x), printf("%lld\n", query(x, root));
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值