#include<bits/stdc++.h> #define Withers using #define AK namespace #define IOI std #define int long long Withers AK IOI;typedef long long ll;typedef pair<int,int>pii;int n,m,u,v,w,x,y,z,l,r,minn=INT_MAX,maxx=INT_MIN,k;int tst;int a[200010];char s[10000010];mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());char t[200010]; #define infll 0x3f3f3f3f3f3f3f3f #define inf 0x3f3f3f3f #define endl '\n' static char buf[1<<18],*paa=buf,*pddd=buf;static char buf2[1<<18],*pppp=buf2; #define getchar()paa==pddd&&(pddd=(paa=buf)+fread(buf,1,1<<18,stdin),paa==pddd)?EOF:*paa++ inline void pc(char ch){if(pppp-buf2==1<<18)fwrite(buf2,1,1<<18,stdout),pppp=buf2;*pppp++=ch;}inline void pcc(){fwrite(buf2,1,pppp-buf2,stdout);pppp=buf2;}inline void rd(int&n){int w=1;register int x(0);register char c(getchar());while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();n=w*x;return;}inline void write(int x){if(x<0)pc('-'),x=-x;static int sta[20];int top=0;do{sta[top++]=x%10,x/=10;}while(x);while(top)pc(sta[--top]+48);}inline void we(int x){write(x);pc('\n');}inline void ws(int x){write(x);pc(' ');} #define YES cout<<"YES"<<endl #define NO cout<<"NO"<<endl #define deb(x)cout<<#x<<"="<<x<<" "; #define pb push_back #define fi first #define se second #define mx3(a,b,c)((a>b?a:b)>c?(a>b?a:b):c) #define mn3(a,b,c)((a<b?a:b)<c?(a<b?a:b):c) #define mem(a,b)memset(a,b,sizeof(a)) #define rep(i,a,b)for(int i=a;i<=b;i++) int pre[10000010]; const int mod=998244353; stack<int> sta; void rd(int n,int a[]){for(int i=1;i<=n;i++)rd(a[i]);}void rda(){rd(n);for(int i=1;i<=n;i++)rd(a[i]);}void adde(vector<pii>g[],int x,int y,int z){g[x].push_back({y,z});g[y].push_back({x,z});} int get(char s[]){char ch=' ';int cnt=0;int len=strlen(s+1);for(int i=1;i<=len;i++)s[i]=0;while(ch==' '||ch=='\n'||ch=='\r'||ch==EOF)ch=getchar();while(!(ch==' '||ch=='\n'||ch=='\r'||ch==EOF))s[++cnt]=ch,ch=getchar();return cnt;}void put(char s[]){int len=strlen(s+1);for(int i=1;i<=len;i++)pc(s[i]);}void get(string&s){s="";char ch=' ';while(ch==' '||ch=='\n'||ch=='\r'||ch==EOF)ch=getchar();while(!(ch==' '||ch=='\n'||ch=='\r'||ch==EOF))s.push_back(ch),ch=getchar();return;}void put(string s){int len=s.size();for(int i=0;i<len;i++)pc(s[i]);}void file(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);} void solve() { //do something rd(n); get(s); rep(i,1,n) { if(s[i]=='W') pre[i]=pre[i-1]-1; else pre[i]=pre[i-1]+1; } //i - j //pre[j]-pre[i-1]>=0 //zhaodiyige x>=i pre[x]-pre[i-1]<0 ans+=x-i pre[x]<pre[i-1] //youbiandiyige < sta.push(n); int ans=0; for(int i=n;i>=1;i--) { while(!sta.empty()) { int p=sta.top(); if(pre[p]>=pre[i-1]) sta.pop(); else break; } if(!sta.empty()) ans+=max((sta.top()-i-1),0ll); else ans+=(n-i); ans%=mod; //if(!sta.empty())cout<<i<<" "<<ans<<" "<<sta.top()<<endl; sta.push(i-1); //cout<<sta.top()<<" " } cout<<ans%mod<<endl; } void multi() { //rd(tst); tst=1; while(tst--) { solve(); } pcc(); } signed main() { ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); multi(); } // POWERED BY WITHERS // THINK ONCE, CODE TWICE /*things to check 1. int overflow or long long memory need 2. recursion/array/binary search/dp/loop bounds 3. precision 4. special cases(n=1,bounds) 5. delete debug statements 6. initialize(especially multi-tests) 7. = or == , n or m ,++ or -- , i or j , > or >= , < or <= , - or = 8. keep it simple and stupid 9. do not delete, use // instead 10. operator priority 11. is there anything extra to output? 12. if you don't know where the bug is , try to clear some parts of the code and check each part seperately. 13. ... */ /* something to think about 1. greedy? dp? searching? dp with matrix/ segment tree? binary search? 2. If contains "not", why not 正难则反 or few affect? */ // lgvc bilibilitdasc zxx zy graygoo tt cyx wsc akioi!
Note.ms
/481851