B. Substring Removal Educational Codeforces Round 57 (Rated for Div. 2)
B. Substring Removal
题意:让你删除字串,删除完之后的剩余串,字符一定是相等的
特殊的是剩余一个字符也是可以的,也可以全删,输出所有满足上述的方案数
代码:
#include <iostream>
#include <cstring>
using namespace std;
#define int long long
#define mod 998244353
signed main(){
//cout << "Hello world!" << endl;
int n;
cin>>n;
string s;
cin>>s;
int l=1;
int r=1;
for(int i=0;i<n;i++){/*左边最长长度*/
if(s[i]==s[i+1])
l++;
else
break;
}
for(int i=n-1;i>=0;i--){/*右边最长长度*/
if(s[i]==s[i-1])
r++;
else
break;
}
int ans=1;/*全删*/
if(s[0]==s[n-1])/*左右相等话复杂一点,删除中间那一段也是一次,自己画画吧*/
ans+=(r+l+r*l)%mod;/* r*l 左右不用贴着删,但是至少要保留一个字符*/
else/*左右不等很简单,自己模拟一遍*/
ans+=(l+r)%mod;/*贴着左边依次往右扩展(r),贴着右边依次往左边扩展(l)*/
cout<<ans<<endl;
return 0;
}
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].