helloworld

更正:第三组:不存在相同的字符|str|=26,26<=n<=100

/*
  70分代码,剩下30%数据没辙了
  前40%找规律加暴力,30%字符串无重复的用动态规划做
  设f[i]为前i位的方案数 可得f[i]=f[i-1]*26,当i>=len时,f[i]-=f[i-len] 
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#define M 100010
#define mod 1000000007
using namespace std;
char s[M],a[M];
int n,len,tot;long long f[M];
bool check(int x)
{
    for(int i=1;i<=len;i++)
      if(s[i]!=a[i+x-1])return false;
    return true;
}
void dfs(int t)
{
    if(t-1>=len)
      if(check(t-len))return;
    if(t>n)
    {
        tot++;tot%=mod;
        return;
    }
    for(int i=1;i<=26;i++)
    {
        a[t]=char(i+'a'-1);
        dfs(t+1);
    }
}
int main()
{
    //freopen("jh.in","r",stdin);
    freopen("helloworld.in","r",stdin);
    freopen("helloworld.out","w",stdout);
    while(scanf("%d",&n)!=EOF)
    {
        scanf("%s",s+1);
        len=strlen(s+1);
        if(len==1)
        {
            long long ans=1;
            for(int i=1;i<=n;i++)
              ans*=25,ans%=mod;
            printf("%d
",(int)ans%mod);
            continue;
        }
        if(n<=5)
        {
            tot=0;
            dfs(1);
            printf("%d
",tot);
            continue;
        }
        f[0]=1;
        for(int i=1;i<=n;i++)
        {
            f[i]=f[i-1]*26;f[i]%=mod;
            if(i>=len)f[i]=((f[i]-f[i-len])%mod+mod)%mod;
        }
        printf("%d
",(int)f[n]%mod);
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/harden/p/5934976.html