给孩子整抑郁了,求大佬看看
查看原帖
给孩子整抑郁了,求大佬看看
1123721
lishunjie楼主2024/5/27 20:45
#include<bits/stdc++.h>
#define N 100005
using namespace std;
int now,lon,n,m;
struct node{
	int a[26],front,len,nt;//a指向后一棵树,front指向前一棵,len代表当前字符串长度,nt表示目前属于哪一个字符
}tree[10000010];
string s;
void insert(string s,int n){
	now=0;
	int qq=s.size();
	for(int i=0;i<qq;i++){
		if(tree[now].a[s[i]-'a']!=0){
			now=tree[now].a[s[i]-'a'];
		}
		else{
			tree[now].a[s[i]-'a']=lon+1;
			tree[lon+1].front=now;
			tree[lon+1].len=tree[now].len+1;
			tree[lon+1].nt=n;
			now=lon+1;
			lon++;
		}
	}
}
int main(){
    cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>s;
	    insert(s,i);	
	}
	now=0;
	lon=0;
	cin>>s;
	int rrt=s.size();
	for(int i=0;i<rrt;i++){
		if(s[i]>='a'&&s[i]<='z'){
			if(tree[now].a[s[i]-'a']!=0){
			//cout<<tree[now].a[s[i]-'a']<<" ";
				lon++;
				now=tree[now].a[s[i]-'a'];
			}else {
				lon++;
			}
			
		}else
		if(s[i]=='E'){
			if(lon==tree[now].len) cout<<tree[now].nt<<" ";
			else cout<<-1<<" ";
			now=0;
			lon=0;
		}else 
		if(s[i]=='B'){
			if(lon!=0)
			lon--;
			while(lon<tree[now].len) {
				now=tree[now].front;
			}
		}else
	    if(s[i]=='T'){
	    	if(lon==tree[now].len){
			int zc=0,wz=0;
			for(int i=0;i<=25;i++){
			   if(tree[now].a[i]!=0) {zc++;wz=i;}
			}
			while(zc==1&&tree[tree[now].a[wz]].nt==tree[now].nt){
				now=tree[now].a[wz];
				lon++;
				zc=0;
				wz=0;
				for(int i=0;i<=25;i++){
	            if(tree[now].a[i]!=0) {zc++;wz=i;}
			}
			}
		}
		//cout<<now<<" "<<tree[now].nt;
		}
	}
	return 0;
}
2024/5/27 20:45
加载中...