zl程序教程

您现在的位置是:首页 >  其它

当前栏目

LOJ10049_基金012498

基金
2023-06-13 09:14:59 时间

大家好,又见面了,我是你们的朋友全栈君。

LOJ10049

Trie裸题(虽然我写这SB题写了快一小时) 对字符串建树的同时刷答案,有两种情况:

  • 经过了一个字符串的词尾(即过了一个标记点)
  • 一直在另一个字符串里走(没有让树长出新枝)
#include<bits/stdc++.h>
#define gt() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++)
#define isnum(ch) (ch>='0'&&ch<='9')
#define __R register
using namespace std;
static char buf[1000000],*p1=buf,*p2=buf;
const char Ans[2][10]={
{
"YES\n"},{
"NO\n"}};
const int maxn=(1e5)+5,maxl=15;
int T,n,tot;int s[maxn][maxl];bool flg,id[maxn];
int read(){
int ret=0;char ch=gt();
while(ch<'0'||ch>'9') ch=gt();
while(ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=gt();
return ret;
}
int main(){
T=read();
while(T--){
n=read(),tot=flg=0;__R char ch=gt();
__R int rot=0,i;__R bool tag,pas;for(id[0]=i=0;i<10;i++) s[rot][i]=0;
while(n--){
while(!isnum(ch)) ch=gt();
rot=tag=pas=0;
while(isnum(ch)){
if(!flg) if(s[rot][ch-'0']) pas=(id[rot=s[rot][ch-'0']]?1:0);else for(id[rot=s[rot][ch-'0']=++tot]=0,tag=1,i=0;i<10;i++) s[rot][i]=0;
ch=gt();if(!isnum(ch)) id[rot]=1;
}
if(!tag||pas) flg=1;
}
fwrite(Ans[flg],1,4-flg,stdout);
}
return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/186955.html原文链接:https://javaforall.cn