• C. Binary Strings are Fun


    Problem - C - Codeforces

    思路:我们发现如果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

    1. // Problem: C. Binary Strings are Fun
    2. // Contest: Codeforces - Codeforces Round 838 (Div. 2)
    3. // URL: https://codeforces.com/contest/1762/problem/C
    4. // Memory Limit: 256 MB
    5. // Time Limit: 1000 ms
    6. #include
    7. #include
    8. #include
    9. #define fi first
    10. #define se second
    11. #define i128 __int128
    12. using namespace std;
    13. typedef long long ll;
    14. typedef double db;
    15. typedef pair<int,int> PII;
    16. const double eps=1e-7;
    17. const int N=5e5+7 ,M=5e5+7, INF=0x3f3f3f3f,mod=1e9+7,mod1=998244353;
    18. const long long int llINF=0x3f3f3f3f3f3f3f3f;
    19. inline ll read() {ll x=0,f=1;char c=getchar();while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
    20. while(c>='0'&&c<='9') {x=(ll)x*10+c-'0';c=getchar();} return x*f;}
    21. inline void write(ll x) {if(x < 0) {putchar('-'); x = -x;}if(x >= 10) write(x / 10);putchar(x % 10 + '0');}
    22. inline void write(ll x,char ch) {write(x);putchar(ch);}
    23. void stin() {freopen("in_put.txt","r",stdin);freopen("my_out_put.txt","w",stdout);}
    24. bool cmp0(int a,int b) {return a>b;}
    25. template<typename T> T gcd(T a,T b) {return b==0?a:gcd(b,a%b);}
    26. template<typename T> T lcm(T a,T b) {return a*b/gcd(a,b);}
    27. void hack() {printf("\n----------------------------------\n");}
    28. int T,hackT;
    29. int n,m,k;
    30. int f[N];
    31. char str[N];
    32. void solve() {
    33. n=read();
    34. scanf("%s",str+1);
    35. f[1]=1;
    36. for(int i=2;i<=n;i++) {
    37. if(str[i]!=str[i-1]) f[i]=1;
    38. else f[i]=f[i-1]*2%mod1;
    39. }
    40. ll res=0;
    41. for(int i=1;i<=n;i++) res=(res+f[i])%mod1;
    42. printf("%lld\n",res);
    43. }
    44. int main() {
    45. // init();
    46. // stin();
    47. // ios::sync_with_stdio(false);
    48. scanf("%d",&T);
    49. // T=1;
    50. while(T--) hackT++,solve();
    51. return 0;
    52. }

  • 相关阅读:
    你知道不同U盘在ARM+Linux下的读写速率吗?
    WPF路由事件
    ruoyi-cloud 升级mybatis plus 报错 Invalid bound statement (not found)
    springMVC 拦截器和异常处理器
    1.2 三维场景动态生成正射纹理-人机交互实现区域框选
    充电宝后台被攻击怎么办
    链表:ArrayList, LinkedList
    数据机构——顺序表的基本操作
    多模态预训练模型指北——LayoutLM
    MVC三层架构
  • 原文地址:https://blog.csdn.net/zzzyyzz_/article/details/133076255