B. Substring Removal

You are given a string ss of length nn consisting only of lowercase Latin letters.

A substring of a string is a contiguous subsequence of that string. So, string "forces" is substring of string "codeforces", but string "coder" is not.

Your task is to calculate the number of ways to remove exactly one substring from this string in such a way that all remaining characters are equal (the number of distinct characters either zero or one).

It is guaranteed that there is at least two different characters in ss.

Note that you can remove the whole string and it is correct. Also note that you should remove at least one character.

Since the answer can be rather large (not very large though) print it modulo 998244353998244353.

If you are Python programmer, consider using PyPy instead of Python when you submit your code.

Input

The first line of the input contains one integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the length of the string ss.

The second line of the input contains the string ss of length nn consisting only of lowercase Latin letters.

It is guaranteed that there is at least two different characters in ss.

Output

Print one integer — the number of ways modulo 998244353998244353 to remove exactly one substring from ss in such way that all remaining characters are equal.

字符串模拟

#include <bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
#define mod 998244353
using namespace std;

int main(){
    int n;
    string s;
    cin>>n>>s;
    ll r=1,l=1,mid=0;
    for(int i=n-1;i>=0;i--){
        if(s[i]==s[i-1]) r++;
        else break;
    }
    for(int i=0;i<n;i++){
        if(s[i]==s[i+1]) l++;
        else break;
    }
    mid=n-l-r;
    ll ans=1;
    if(s[0]==s[n-1]){
        ans+=(r+l+r*l)%mod;
    }else{
        ans+=(l+r)%mod;
    }
    cout<<ans%mod;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Endeavor_G/article/details/85336045
今日推荐