2 条题解

  • 0
    @ 2025-10-8 17:09:33
    #include<bits/stdc++.h>
    using namespace std;
    const int N=1e5+10;
    char s[N],t[N];
    int ch[N][26],id,ed[N],pre[N];
    void ins(char *s)
    {
        int p=0,len=strlen(s);
        for(int i=0;s[i];i++)
        {
            int j=s[i]-'a';
            if(ch[p][j]==0) ch[p][j]=++id;
            p=ch[p][j];
        }
        ed[p]=len;
    }
    void build()
    {
        queue<int> Q;
        for(int i=0;i<26;i++)if(ch[0][i])Q.push(ch[0][i]);
        while(!Q.empty())
        {
            int x=Q.front();Q.pop();
            for(int i=0;i<26;i++)
            {
                int &y=ch[x][i];
                if(y==0)     y=ch[pre[x]][i];
                else    pre[y]=ch[pre[x]][i], Q.push(y); 
            }
        }
    }
    int a[N],b[N];
    void query(char *s)
    {
        int p=0,tp=0; 
        memset(b,0,sizeof(b));
        for(int i=0;s[i];i++)
        {
            p=ch[p][s[i]-'a'];
            tp++;a[tp]=i;b[tp]=p;
            if(ed[p])
            {
                tp-=ed[p];
                p=b[tp];
            }
        }
        for(int i=1;i<=tp;i++) printf("%c",s[a[i]]);
    }
    int main()
    {
        scanf("%s",s);
        int n;scanf("%d",&n);
        id=0;memset(ch,0,sizeof(ch));memset(ed,0,sizeof(ed)); 
        for(int i=1;i<=n; i++)scanf("%s",t),ins(t);
        memset(pre,0,sizeof(pre));build();
        query(s);
        return 0;
    }
    
    • 0
      @ 2025-10-8 17:09:20
      #include<bits/stdc++.h>
      using namespace std;
      const int N=1e5+10;
      char s[N],t[N];
      int ch[N][26],id,ed[N],pre[N];
      void ins(char *s)
      {
          int p=0,len=strlen(s);
          for(int i=0;s[i];i++)
          {
              int j=s[i]-'a';
              if(ch[p][j]==0) ch[p][j]=++id;
              p=ch[p][j];
          }
          ed[p]=len;
      }
      void build()
      {
          queue<int> Q;
          for(int i=0;i<26;i++)if(ch[0][i])Q.push(ch[0][i]);
          while(!Q.empty())
          {
              int x=Q.front();Q.pop();
              for(int i=0;i<26;i++)
              {
                  int &y=ch[x][i];
                  if(y==0)     y=ch[pre[x]][i];
                  else    pre[y]=ch[pre[x]][i], Q.push(y); 
              }
          }
      }
      int a[N],b[N];
      void query(char *s)
      {
          int p=0,tp=0; 
          memset(b,0,sizeof(b));
          for(int i=0;s[i];i++)
          {
              p=ch[p][s[i]-'a'];
              tp++;a[tp]=i;b[tp]=p;
              if(ed[p])
              {
                  tp-=ed[p];
                  p=b[tp];
              }
          }
          for(int i=1;i<=tp;i++) printf("%c",s[a[i]]);
      }
      int main()
      {
          scanf("%s",s);
          int n;scanf("%d",&n);
          id=0;memset(ch,0,sizeof(ch));memset(ed,0,sizeof(ed)); 
          for(int i=1;i<=n; i++)scanf("%s",t),ins(t);
          memset(pre,0,sizeof(pre));build();
          query(s);
          return 0;
      }
      • 1

      信息

      ID
      5605
      时间
      100ms
      内存
      256MiB
      难度
      7
      标签
      递交数
      74
      已通过
      20
      上传者