2 条题解

  • 0
    @ 2025-10-8 17:02:20
    #include <bits/stdc++.h>
    using namespace std;
    const int N = 65, P = 1e4 + 7;
    char s[110];
    int ch[N * 100][26], id, ed[N * 100], pre[N * 100], f[110][N * 100];
    
    void ins(char *s, int x) {
        int p = 0;
        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] = 1;
    }
    
    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();
            ed[x] |= ed[pre[x]];
            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 main() {
        int n, m; scanf("%d%d", &n, &m);
        id = 0; memset(ch, 0, sizeof(ch)); memset(ed, 0, sizeof(ed));
        for (int i = 1; i <= n; i++) {
            scanf("%s", s);
            ins(s, i);
        }
        memset(pre, 0, sizeof(pre)); build();
        
        memset(f, 0, sizeof(f)); f[0][0] = 1;
        for (int L = 0; L < m; L++)
            for (int x = 0; x <= id; x++)
                for (int j = 0; j < 26; j++) {
                    int y = ch[x][j];
                    if (ed[y] == 0) f[L + 1][y] = (f[L + 1][y] + f[L][x]) % P;
                }
        int ans = 0;
        for (int i = 0; i <= id; i++) ans = (ans + f[m][i]) % P;
        int tm = 1; for (int i = 1; i <= m; i++) tm = tm * 26 % P;
        ans = (tm - ans + P) % P;
        printf("%d\n", ans);
        return 0;
    }
    
    • 0
      @ 2025-10-8 17:02:07
      #include<bits/stdc++.h>
      using namespace std;
      const int N=65,P=1e4+7;
      char s[110];
      int ch[N*100][26],id,ed[N*100],pre[N*100],f[110][N*100];
      void ins(char *s,int x)
      {
          int p=0;
          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]=1;
      }
      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();
              ed[x]|=ed[pre[x]];
              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 main()
      {
          int n,m;scanf("%d%d",&n,&m);
          id=0;memset(ch,0,sizeof(ch));memset(ed,0,sizeof(ed)); 
          for(int i=1;i<=n; i++)
          {
      		scanf("%s",s);
      		ins(s,i);
      	}
          memset(pre,0,sizeof(pre));build();
          
          memset(f,0,sizeof(f));f[0][0]=1;
          for(int L=0;L<m;L++)
          	for(int x=0;x<=id;x++)
          		for(int j=0;j<26;j++)
          		{
          			int y=ch[x][j];
          			if(ed[y]==0)f[L+1][y]=(f[L+1][y]+f[L][x])%P;
          		}
          int ans=0;
          for(int i=0;i<=id;i++)ans=(ans+f[m][i])%P;
          int tm=1;for(int i=1;i<=m;i++)tm=tm*26%P;
          ans=(tm-ans+P)%P;
      	printf("%d\n",ans);
          return 0;
      }
      • 1

      信息

      ID
      2683
      时间
      1000ms
      内存
      128MiB
      难度
      6
      标签
      递交数
      24
      已通过
      12
      上传者