B. Substring Removal(字符串变换)

本文解析了B.SubstringRemoval算法题目,详细介绍了如何计算从给定字符串中移除一个子串后,使得剩余字符相等的方法。文章讨论了输入输出格式、限制条件,并提供了C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. Substring Removal

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples

input

Copy

4
abaa

output

Copy

6

input

Copy

7
aacdeee

output

Copy

6

input

Copy

2
az

output

Copy

3

Note

Let s[l;r]s[l;r] be the substring of ss from the position ll to the position rr inclusive.

Then in the first example you can remove the following substrings:

  • s[1;2]s[1;2];
  • s[1;3]s[1;3];
  • s[1;4]s[1;4];
  • s[2;2]s[2;2];
  • s[2;3]s[2;3];
  • s[2;4]s[2;4].

In the second example you can remove the following substrings:

  • s[1;4]s[1;4];
  • s[1;5]s[1;5];
  • s[1;6]s[1;6];
  • s[1;7]s[1;7];
  • s[2;7]s[2;7];
  • s[3;7]s[3;7].

In the third example you can remove the following substrings:

  • s[1;1]s[1;1];
  • s[1;2]s[1;2];
  • s[2;2]s[2;2].

ac:

#include<bits/stdc++.h>
#define ll long long
#define MAXN 200005
#define mod 998244353
using namespace std;

char str[MAXN]={0};

int main()
{
    ll len,l=1,r=1;
    scanf("%d",&len);
    scanf("%s",&str);
    for(ll i=0;i<len-1;i++)
    {
        if(str[i]==str[i+1])
            l++;
        else
            break;
    }
    for(ll i=len-1;i>=1;i--)
    {
        if(str[i]==str[i-1])
            r++;
        else
            break;
    }
    if(str[0]==str[len-1])//
        printf("%lld\n",((l+1)*(r+1))%mod);
    else printf("%lld\n",(l+r+1)%mod);//左右不等,要从左右顶端开始删,向左l次,向右r次,全删1次
    return 0;
}

 

转载于:https://www.cnblogs.com/wangtao971115/p/10358190.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值