"String Match" uvalive 4670 template title

Source: Internet
Author: User

To a text t, and N template strings, are made up of lowercase letters that ask those strings the most occurrences in a string, the maximum number of outputs, and the corresponding string.

The template problem of AC automata, the recursive output of the time to change to the cumulative number of statistics array cnt can be.

Big petition think there will be duplicate templates, but in the actual test, do not judge the repetition can also pass.

#include <bits/stdc++.h> #define EPS 1e-9#define for (i,j,k) for (int i=j;i<=k;i++) #define MAXN 10505#define MAXM 40005#define INF 0x3fffffffusing namespace Std;typedef long long ll;int i,j,k,n,m,x,y,t,ans,big,cas,num,w,u,v;bool flag ; char P[155][75];char t[1000006];struct acauto{int ch[maxn][26];int size;int F[MAXN],LAST[MAXN],VAL[MAXN],CNT[MAXN]; void Init () {Size=1;memset (ch[0],0,sizeof (ch[0])); Memset (cnt,0,sizeof (CNT));} int idx (char c) {return C ' a ';} void Insert (char *s,int v) {int U=0,len=strlen (s), for (int i=0;i<len;i++) {int c=idx (s[i]), if (!ch[u][c]) {memset (ch[ Size],0,sizeof (Ch[size])); val[size]=0;ch[u][c]=size++;} U=CH[U][C];} Val[u]=v;} void print (int j) {if (j) {cnt[val[j]]++;p rint (Last[j]);}} int Getfail () {queue <int> q;f[0]=0;for (int c=0;c<26;c++) {int u=ch[0][c];if (u) {f[u]=0;q.push (u); last[u]=0;}} while (!q.empty ()) {int R=q.front (), Q.pop (), for (int c=0;c<26;c++) {int u=ch[r][c];if (!u) {ch[r][c]=ch[f[r]][c]; Continue;} Q.push (u); int v=f[r];f[u]=ch[v][c];last[u]=val[F[u]]?f[u]:last[f[u]];}} void Find (char *t) {int N=strlen (T), int j=0;for (int i=0;i<n;i++) {int c=idx (T[i]), while (J&&!ch[j][c]) j=f[j] ; J=ch[j][c];if (Val[j]) print (j); else if (Last[j]) print (Last[j]);}} Ac;int Main () {while (scanf ("%d", &n), N) {ac.init (); for (i=1;i<=n;i++) {scanf ("%s", P[i]); Ac.insert (p[i],i);} Ac.getfail (); scanf ("%s", T), Ac.find (t); Big=0;for (i=1;i<=n;i++) {Big=max (big,ac.cnt[i]);} printf ("%d\n", Big), for (i=1;i<=n;i++) {if (ac.cnt[i]==big) printf ("%s\n", P[i]);}} return 0;}

  

"String Match" uvalive 4670 template title

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.