

思路:我们发现如果si==si-1,那么假设为00,用f[i-1]表示si-1能够组成的合法的个数,那么si-1扩展的字符串中至少会有i-1个0,那么si有两种放法,1种是放一个1,一种是放一个0,如果放1的话会会形成010,后面两个抵消了,所以最终还是0多着,满足题意,如果放0,那么肯定也是0多着满足题意,所以如果相等,那么f[i]=2*f[i-1],如果不相等,那么只有一种放法,假设为01,那么因为si-1是合法的,那么生成的字符串中起码0比1多1,所以这一位必须放1,并且又因为si-1中0比1恰好多1只会有一种方案(如果前面只有1个0的话那么只有一种方案,如果有多个连续的0的话,因为由上面我们能够知道,当相等的时候可以放0也可以放1,如果只多1的情况的话就是必须每次都放1才能够恰好多1,所以只会有1种方案),所以f[i]=1
- // Problem: C. Binary Strings are Fun
- // Contest: Codeforces - Codeforces Round 838 (Div. 2)
- // URL: https://codeforces.com/contest/1762/problem/C
- // Memory Limit: 256 MB
- // Time Limit: 1000 ms
-
- #include
- #include
- #include
- #define fi first
- #define se second
- #define i128 __int128
- using namespace std;
- typedef long long ll;
- typedef double db;
- typedef pair<int,int> PII;
- const double eps=1e-7;
- const int N=5e5+7 ,M=5e5+7, INF=0x3f3f3f3f,mod=1e9+7,mod1=998244353;
- const long long int llINF=0x3f3f3f3f3f3f3f3f;
- inline ll read() {ll x=0,f=1;char c=getchar();while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
- while(c>='0'&&c<='9') {x=(ll)x*10+c-'0';c=getchar();} return x*f;}
- inline void write(ll x) {if(x < 0) {putchar('-'); x = -x;}if(x >= 10) write(x / 10);putchar(x % 10 + '0');}
- inline void write(ll x,char ch) {write(x);putchar(ch);}
- void stin() {freopen("in_put.txt","r",stdin);freopen("my_out_put.txt","w",stdout);}
- bool cmp0(int a,int b) {return a>b;}
- template<typename T> T gcd(T a,T b) {return b==0?a:gcd(b,a%b);}
- template<typename T> T lcm(T a,T b) {return a*b/gcd(a,b);}
- void hack() {printf("\n----------------------------------\n");}
-
- int T,hackT;
- int n,m,k;
- int f[N];
- char str[N];
-
- void solve() {
- n=read();
- scanf("%s",str+1);
-
- f[1]=1;
-
- for(int i=2;i<=n;i++) {
- if(str[i]!=str[i-1]) f[i]=1;
- else f[i]=f[i-1]*2%mod1;
- }
-
- ll res=0;
- for(int i=1;i<=n;i++) res=(res+f[i])%mod1;
-
- printf("%lld\n",res);
- }
-
- int main() {
- // init();
- // stin();
- // ios::sync_with_stdio(false);
-
- scanf("%d",&T);
- // T=1;
- while(T--) hackT++,solve();
-
- return 0;
- }