1096B - Substring Removal

版权声明:大家一起学习,欢迎转载,转载请注明出处。若有问题,欢迎纠正! https://blog.csdn.net/memory_qianxiao/article/details/85336562

题意:给了长度为n的字符,删除其中的字符,让剩下的字符一样,就满足条件,输出有多少种删法,全部删除,留一个字符也满足条件。

题解:先统计前面连续相同的字符个数,再统计后面连续相同的字符的个数。两种情况,首尾字符相同,就是两种字符加1的积,不相同就是和加1.c要注意,乘法会溢出,也就是爆int,类型longlong,python就无所谓了。

B. Substring Removal

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

扫描二维码关注公众号,回复: 4796067 查看本文章

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].

c++:

#include<bits/stdc++.h>
using namespace std;
int mod=998244353,n,l=1,r=1;
int main()
{
    string s;
    cin>>n>>s;
    for(int i=1; i<n; i++)
        if(s[i]==s[i-1]) l++;
        else break;
    for(int i=n-1; i>=1; i--)
        if(s[i]==s[i-1]) r++;
        else break;

    if(s[0]!=s[n-1])
        cout<<l+r+1<<endl;
    else cout<<(l+1)*(r+1)%mod<<endl;
    return 0;
}

python:

n=int(input());s=input()
l=1;r=1;mod=998244353
while(s[l]==s[l-1]):l+=1
while(s[n-r]==s[n-r-1]):r+=1
if s[0]!=s[-1]:print((l+r+1)%mod)
else: print((l+1)*(r+1)%mod)
          

猜你喜欢

转载自blog.csdn.net/memory_qianxiao/article/details/85336562
今日推荐