捞
  • 板块灌水区
  • 楼主huchaojian
  • 当前回复4
  • 已保存回复4
  • 发布时间2024/4/15 17:48
  • 上次更新2024/4/15 20:17:54
查看原帖
捞
1082242
huchaojian楼主2024/4/15 17:48

rt求调代码(题目P3769) 代码如下:

#include<bits/stdc++.h>
using namespace std;
map<int,string>ma;
int tot,u,c,nxt[500005],trie[500005][30],b1[500005],v;
bool b[500005];
queue<int>q;
void in(string s){
	u=1;
	int len=s.length();
	for(int i=0;i<len;i++){
		c=s[i]-'a';
		if(trie[u][c]!=0){
			tot++;
			trie[u][c]=tot;
			memset(trie[u],0,sizeof(trie[u]));
		}
		u=trie[u][c];
	}
	ma[u]=s;
	b[u]=1;
}
void fail(){
	int v;
	for(int i=0;i<26;i++){
		if(trie[0][i]){
			q.push(trie[0][i]);
			nxt[trie[0][i]]=0;
		}
	}
	while(!q.empty()){
		u=q.front();
		q.pop();
		for(int i=0;i<=25;i++){
			v=trie[u][i];
			if(trie[u][i]) trie[u][i]=trie[nxt[u]][i];
			else{
				q.push(v);
				v=nxt[u];
			//	while(v!=0&&!trie[v][c]) v=nxt[v];
				nxt[trie[u][i]]=trie[v][i];
			}
		}
	}
}
void find(string s){
	int len=s.length();
	u=1;
	for(int i=0;i<len;i++){
		c=s[i]-'a';
		if(trie[u][c]){
		if(b[trie[u][c]]) b1[trie[u][c]]++;
		v=nxt[trie[u][c]];
		while(trie[v][c]!=0&&v) {
		if(b[trie[v][c]])	b1[trie[v][c]]++;
		v=nxt[trie[v][c]];	
		}
		u=trie[u][c];
		}
	}
}
string safegetline() {
	do {
		string s;
		getline(cin, s);
		if (!s.empty()) {
			return s;
		}
	}while(1);
}
int main(){ 
	string s1;
	int maxx,ans;
	int t;
	memset(trie,0,sizeof(trie));
	scanf("%d",&t);
	while(t!=0){
		maxx=0;
		tot=1;
		for(int i=1;i<=t;i++){
			s1 = safegetline();
			in(s1);
		}
		fail();
//		getline(cin,s1);
		s1 = safegetline();
		find(s1);
		for(int i=1;i<=tot;i++){
		for(int j=0;j<=25;j++){
			trie[i][j]=0;
		}
		if(maxx<=b1[i]){
			maxx=b1[i];
			ans=i;
		}
		nxt[i]=0;
		b[i]=0;
		b1[i]=0;
		}
		cout<<maxx<<endl;
		cout<<ma[ans]<<endl;
		scanf("%d",&t);
	}
}
2024/4/15 17:48
加载中...