题意
给一个数字n,表示一个排列长度为n。
给一个数字m,表示m此操作
给一个字符串s,只包含><?
有一个空集合,最开始往里面加入一个数字。
然后按照字符串s往里加数字,
1.>
表示加入的数字比当前集合所有数字大,
2.<
表示加入的数字比当前集合所有的数字小,
3.?
表示不是最大也不是最小
求每次更改完,插入数字的方案数目
解法
倒着看字符串s,假设此时已经插入了x个数字(包括当前即将被插入的数字),并且当前位置是>
,那么,当前被插入的数字选法唯一,如果是?
,那么当前数字可以有x-2种选法(不可以选最大和最小)。
代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
typedef long long ll;
typedef double db;
ll a[N];
ll n;
const ll mol=998244353;
ll m;
ll qmi(ll a,ll b) {
ll res=1;
while(b) {
if(b&1)
res=res*a%mol;
a=a*a%mol;
b>>=1;
}
return res;
}
void solve() {
cin>>n>>m;
string s;
cin>>s;
ll ans=1;
for(int i=1; i<s.size(); i++) {
if(s[i]=='?') {
ans=ans*i%mol;
}
}
if(s[0]=='?'){
cout<<0<<endl;
}
else
cout<<ans<<endl;
while(m--) {
int pos;
char ss;
cin>>pos>>ss;
pos--;
if(pos!=0) {
if(s[pos]=='?'&&ss!='?') {//少一个?
ans=ans*qmi(pos,mol-2)%mol;
} else if(s[pos]!='?'&&ss=='?') {//多一个?
ans=ans*pos%mol;
}
}
s[pos]=ss;
if(s[0]=='?') {
cout<<0<<endl;
} else {
cout<<ans<<endl;
}
}
}
int main() {
int t=1;
// cin>>t;
while(t--)
solve();
}