社区讨论

求助

灌水区参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@m4kw6e0m
此快照首次捕获于
2024/12/12 13:41
去年
此快照最后确认于
2024/12/12 13:48
去年
查看原帖
题目是P1481
代码
CPP
#include<bits/stdc++.h>
using namespace std;
struct node{
	char c;
	int child[130]={};
	int end=0;
}root;
vector<node> trie;
int ans;
void build(string s){
	int fa=0,cnt=1;
	node x;
	for(int i=0;i<s.size();i++){
		if(!trie[fa].child[s[i]]){
			int l=trie.size();
			trie[fa].child[s[i]]=l;
			x.c=s[i];
			if(i==s.size()-1)x.end=cnt;
			trie.push_back(x);
			fa=l;
		}
		else{
			fa=trie[fa].child[s[i]];
			if(trie[fa].end){
				cnt++;
				ans=max(ans,cnt);
			}
		}
	} 
}
int main(){
	int n;
	string s;
	cin>>n;
	trie.push_back(root);
	for(int i=0;i<n;i++){
		cin>>s;
		build(s);
	}
	cout<<ans<<endl;
	return 0;
}
请问:为什么我的node的end不赋值=0,答案就不会对呢?(我跟着视频学的,视频里面没有给end赋值0但是对了)

回复

0 条回复,欢迎继续交流。

正在加载回复...