QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#780085#9747. 字符串复制Nlll#TL 2ms11808kbC++171.9kb2024-11-25 00:46:322024-11-25 00:46:34

Judging History

你现在查看的是最新测评结果

  • [2024-11-25 00:46:34]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:11808kb
  • [2024-11-25 00:46:32]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int Maxn = 2e6 + 5;
const ll P = 998244353;
struct node{
    int len, fa;
    int s[26];
};
struct SAM{
    int n,lst=1,cnt=1;
    char s[Maxn];
    ll ans[Maxn << 1];
    node a[Maxn << 1];
    void ins(int c)
    {
	    int p=lst,np=lst=++cnt;a[np].len=a[p].len+1;
	    for(;p&&!a[p].s[c];p=a[p].fa) a[p].s[c]=np;
	    if(!p) return(void)(a[np].fa=1);
	    int q=a[p].s[c];
	    if(a[q].len==a[p].len+1) return(void)(a[np].fa=q);
	    int nq=++cnt;a[nq]=a[q],a[nq].len=a[p].len+1,a[q].fa=a[np].fa=nq;
	    for(;p&&a[p].s[c]==q;p=a[p].fa) a[p].s[c]=nq;
    }
    ll dfs(int x)
    {
	    //if(ans[x]) return ans[x];
        ans[x] = 0;
	    for(int i=0;i<26;i++)
        {
            if(a[x].s[i])
            {
                ans[x] = (ans[x] + dfs(a[x].s[i]) + 1) % P;
            }
        }
	    return ans[x];
    }
    ll num()
    {
        return dfs(1);
    }
    void ins(string s)
    {
        for(int i = 0; i < s.length(); i++)
        {
            ins(s[i] - 'a');
        }
    }
};
SAM S, SS, SSS, tmp;
void solve()
{
    int n;
    ll m;
    cin >> n >> m;
    string s;
    cin >> s;
    S.ins(s);
    ll ans = 0;
    if(m == 1)
    {
        ans = S.num();
    }
    else
    {
        ll x = S.num();
        S.ins(s);
        ll y = S.num();
        ll z = 0;
        int now = 2;
        if(m > 2)
        {
            while(1)
            {
                S.ins(s);
                z = S.num();
                now++;
                if((z + x - y * 2) % P == 0 || now == m)
                {
                    break;
                }
            }
            ans = ((z - y) * (m - now) % P + z) % P;
        }
        else ans = y;
    }
    cout << ans << "\n";
}
int main()
{
	cin.tie(0)->sync_with_stdio(false);
    solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 11808kb

input:

6 2
mantle

output:

57

result:

ok single line: '57'

Test #2:

score: 0
Accepted
time: 1ms
memory: 11744kb

input:

12 1919810
ifamjlifamjl

output:

138226305

result:

ok single line: '138226305'

Test #3:

score: -100
Time Limit Exceeded

input:

13 935330878
aabbbbababbaa

output:


result: