Love letter, Luo Guzhi's experience of improvement, strong connected component

The subject

      give you a love letter

      This question is a bare dictionary tree.

      For a sentence, we extract each word and put it into the dictionary tree. If there is a completely matching word (mark the last node when saving the word), then we add one and output ans, be careful to open one bool array to store whether the word has occurred.

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;

int n;
int tot = 0;
int son[5010][30];
int c[5010];
bool tf[5010];

struct Trie{
	void Clear(){tot=1;memset(son[1],0,sizeof(son[1]));memset(c,0,sizeof(c,0,sizeof(c)));}
	void Insert(char *s,int x){
		int n=strlen(s);
		int u = 1;
		for(int i=0;i<n;i++){
			int now=s[i]-'a';
			if(son[u][now]==0){
				son[u][now]=++tot;
				memset(son[tot],0,sizeof(son[tot]));
			}
			u=son[u][now];
		}
		c[u]=x;
	}
	int Find(char *s){
		int n=strlen(s);
		int u = 1;
		for(int i=0;i<n;i++){
			int now=s[i]-'a';
			if(son[u][now]!=0)
				u=son[u][now];
			else return 0;
		}
		return c[u];
	}
}xx;

int main(){
	scanf("%d",&n);
	char s[1010];
	xx.Clear();
	for(int i=1;i<=n;i++){
		scanf("%s",s);
		int yu=strlen(s);
		for(int j=0;j<yu;j++)
			if(s[j]>='A' && s[j]<='Z') s[j]=s[j]-'A'+'a';
		xx.Insert(s,i);
	}
	scanf("\n");
	gets(s);
	int n=strlen(s);
	for(int i=0;i<n;i++)
		if(s[i]>='A' && s[i]<='Z') s[i]=s[i]-'A'+'a';
	char op[1010];
	int tt = 0;
	tf[0]=true;
	int ans=0;
	for(int i=0;i<n;i++){
		if(s[i]>='a' && s[i]<='z') op[tt++]=s[i];
		else {
			on [tt] = '\ 0';
			int p=xx.Find(op);
			if(!tf[p]){
				tf[p]=true;
				years++;	
			}
			if(s[i]=='.'){
				for(int i=1;i<=n;i++)
					tf[i]=false;
				tf[0]=true;
			}
			tt=0;
		}
	}
	printf("%d\n",ans);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325977841&siteId=291194637