1096B,
1096B,
题意:给了长度为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
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)
相关文章
- 暂无相关文章
用户点评