[ABC313E] Duplicate 题解
考虑什么时候无解。显然如果有两个相邻字符都不等于
那么其他情况是否都是有解的?答案是肯定的。考虑这样的字符串有什么性质:它必然是由单个大于
至于计数,考虑一次操作会对每个
注意特判最后一段
放代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod=998244353;
main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n,c=0; string s; cin>>n>>s;
for(int i=1;i<n;i++)
if(min(s[i],s[i-1])>49)
cout<<"-1\n",exit(0); // 判断无解
int r=n-1;
while(r>0){
if(r>0&&s[r]>49){
(++c)%=mod; // 加上删除它本身一位的贡献
int x=0,d=s[r]-48; r--;
while(r>0&&s[r]==49)r--,x++; // 找极长 1 连续段
(c+=c*(d-1)%mod+x%mod)%=mod; // 算贡献
}
else while(r>0&&s[r]==49)r--,(++c)%=mod; // 特判
}
cout<<c<<endl;
return 0;
}