The 2019 ICPC Asia Shanghai Regional Contest--Prefix Code()

 检查是否有一个数是其他数的前缀---典型的字典树模板

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
typedef struct node{
	bool flag;//标记字符串结尾 
	int CNT=0;//记录节点 
	int next_s[10];//指针 
}tire;
tire a[100010];
char s[11];
int cnt;
//初始化字典树 
void init(){
	cnt=0;
	a[0].flag=false;
	memset(a[0].next_s,-1,sizeof(a[0].next_s));
	a[0].CNT=0;
}
//插入字符串 
bool Insert(){
	int id=0;
	for(int i=0;s[i];i++){
		if(a[id].next_s[s[i]-'0']==-1){
			if(a[id].flag)return false;//
			a[id].next_s[s[i]-'0']=++cnt;
			id=cnt;
			a[cnt].flag=false;
			a[id].CNT=1;
			memset(a[cnt].next_s,-1,sizeof(a[cnt].next_s));
		}
		else {
			id=a[id].next_s[s[i]-'0'];
			a[id].CNT++;
		}
	}
	a[id].flag=true;
	//是否出现过 
	if(a[id].CNT>1)return false;
	else return true;
}

int main(){
	int t,n,num;
	int Case=0;
	scanf("%d",&t);
	while(t--){
		init();
		bool flag=true;
		scanf("%d",&n);
		for(int i=0;i<n;i++){
			scanf("%s",s);
			if(flag&&Insert())
			;
			else flag=false;
		}
		if(flag)printf("Case #%d: Yes\n",++Case);
		else printf("Case #%d: No\n",++Case);
	}
	return 0;
}
//            /\       |  /  |**、
//			 /  \      | /   |   \
//			/    \     |/    |   /  _____                      ____   |  /
//		   /------\    |\    |__/  /     \  \      /\      /  /    \  | /
//		  /        \   | \   |    /       \  \    /  \    /  /______\ |/
//		 /          \  |  \  |    \       /   \  /    \  /   \        |
//      /            \ |   \ |     \_____/     \/      \/     \_____  |
/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
// warm heart, wagging tail,and a smile just for you!
//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                           O\  =  /O
//                        ____/`---'\____
//                      .'  \|     |//  `.
//                     /  \|||  :  |||//  \
//                    /  _||||| -:- |||||-  \
//                    |   | \\  -  /// |   |
//                    | \_|  ''\---/''  |   |
//                    \  .-\__  `-`  ___/-. /
//                  ___`. .'  /--.--\  `. . __
//               ."" '<  `.___\_<|>_/___.'  >'"".
//              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//              \  \ `-.   \_ __\ /__ _/   .-` /  /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//
发布了186 篇原创文章 · 获赞 38 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43746332/article/details/104342989