SP10293
题意
给定字符串 $s$,求有多少种不同的方法把它切割成若干个连续且字符相同的子串?
思路
以样例 100 为例。
我们可以将其分割为 1 00 或 1 0 0 一共两种情况。
不难发现,当相邻两个字符相同时,则在这两个字符中间可加可不加,所以当前方案数 $\times 2$。
所以统计相邻两个字符相同的数量 $a$,则答案为 $2^a$。
代码
#include<bits/stdc++.h>
using namespace std;
int t,ans;
string s;
int main(){
scanf("%d",&t);
while(t--){
ans=0;
cin>>s;
for(int i=1;i<s.length();i++)ans+=(s[i]==s[i-1]);
printf("%lld\n",1ll<<ans);
}
}

浙公网安备 33010602011771号