电话单POJ3630

电话单POJ3630

思路

题干在这:POJ3630
依次把电话号码插入字典树。如果一个号码插入过程中到了一个结束节点,或者插完这个号码并没有建立新节点,说明是no;否则是yes。

ac代码

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<cstring>
#include<vector>
#define ll long long
#define N 200010
using namespace std;
int tot = 1;
int trie[N][10];
int endi[N];
vector<int> resort;
int main() {
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int sign = 0;
		tot = 1;
		memset(trie, 0, sizeof(trie));
		memset(endi, 0, sizeof(endi));
		int n;
		cin >> n;
		for (int j = 0; j < n; j++) {
			char s[20] = { 0 };
			scanf_s("%s", s, 20);
			if (sign)
				continue;
			int sq = 0;
			int mark = 0;
			for (int k = 0; k < strlen(s); k++) {
				if (trie[sq][s[k] - '0'] == 0) {
					tot++;
					trie[sq][s[k] - '0'] = tot;
					sq = tot;
					mark = 1;
				}
				else {
					sq = trie[sq][s[k] - '0'];
				}
				if (endi[sq] == 1) {
					resort.push_back(0);
					sign = 1;
					break;
				}
				if (k == strlen(s) - 1) {
					if (mark == 0) {
						resort.push_back(0);
						sign = 1;
						break;
					}
					else
						endi[sq] = 1;
				}
			}
		}
		if (sign)
			continue;
		resort.push_back(1);
	}
	for (int i = 0; i < t; i++) {
		if (resort[i])
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值