E - Magical Code Gym - 100482E ——滚动数组+不要强制转换类型

Do you know how magicians cast spells? The spell consists of its name and its magical code. The casting is done by saying its name while keeping in mind the magical code (only then the spell works). Only the most experienced, the smartest and the most talented magicians can create they own spells. They name their spell and determine its purpose. They make some rituals near the Tree of Power. And if the magician is worth the tree creates the magical code for this spell.

However, the tree itself is designed and created by the mathematician who told me the mechanism of creating spells. Here is the algorithm in “our language”:

const int MOD = 1013;

int code = 0;

for (int i = 0; i < spell_name.size(); i++)

code = (code * 31 + spell_name[i]) % MOD;

printf(“You are worth it! Your power is unleashed by the number %d” , code);

The creator of the tree wants to boost your knowledge. So he tells some of the most recent codes and parts of the spell’s name. You need to tell what the maximum number of spells could have this magical code. If there is only one choice - print it!

Note: the spell’s name consists only of lowercase English alphabet letters (’a’-’z’).

Input
The first line contains the number of test cases T (T?≤?20). The first line of each test case contains the magical code, c (0?≤?c?

#include<stdio.h>
#include<cstring>
using namespace std;
#define ll long long
const int mod=1013;
ll dp[1020][2];
int pre[1020][10005];
int ans[1020][10005];
int ct,x,y,i,j,k,len,flag;
char ss[10005];
char s[10005];
int main()
{
    int t,cas=0,n;
    scanf("%d",&t);
    for(int i=0;i<=mod;i++)
        pre[i][1]=-1;
    while(t--)
    {
        scanf("%d",&n);
        scanf("%s",s+1);
        memset(dp,0,sizeof(dp));
        if(s[1]=='?')
        {
            for(i=0;i<26;i++)
            {
                dp[(i+'a')%mod][1]=1;
                ans[(i+'a')%mod][1]=i+'a';
            }
        }
        else
        {
            dp[s[1]%mod][1]=1;
            ans[s[1]%mod][1]=s[1];
        }
        len=strlen(s+1);
        flag=1;
        for(i=2;i<=len;i++)
        {
            flag^=1;
            for(j=0;j<mod;j++)
                dp[j][flag]=0;
            if(s[i]=='?')
            {
                for(j=0;j<mod;j++)
                {
                    if(dp[j][flag^1])
                    {
                        for(k=0;k<26;k++)
                        {
                            dp[(j*31+k+'a')%mod][flag]+=dp[j][flag^1];
                            pre[(j*31+k+'a')%mod][i]=j;
                            ans[(j*31+k+'a')%mod][i]=k+'a';
                        }
                    }
                }
            }
            else
            {
                for(j=0;j<mod;j++)
                {
                    if(dp[j][flag^1])
                    {
                        dp[(j*31+s[i])%mod][flag]+=dp[j][flag^1];
                        pre[(j*31+s[i])%mod][i]=j;
                        ans[(j*31+s[i])%mod][i]=s[i];
                    }
                }
            }
        }
        printf("Case #%d: ",++cas);
        if(dp[n][flag]==1)
        {
            ct=0;
            x=n,y=len;
            while(x!=-1)
            {
                ss[++ct]=ans[x][y];
                x=pre[x][y];
                y--;
            }
            for(i=ct;i>=1;i--)
                printf("%c",ss[i]);
            printf("\n");
        }
        else
            printf("%lld\n",dp[n][flag]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/tianyizhicheng/article/details/82419806