zoukankan      html  css  js  c++  java
  • 【Educational Codeforces Round 81 (Rated for Div. 2) B】Infinite Prefixes

    题目链接

    【题解】

    把0看成是1,把1看成是-1 求一个前缀和。 pre[i] = pre[i-1]+1 得到delta = pre[n] 显然对于每个位置的值pre[i] 再复制一遍s的话。 下一个s的该位置,也即i+n的前缀和显然为pre[i]+delata 那么无限的情况就很显然了。 即pre[i]==x,而且delata==0. 只要出现一个这种情况,就是无限。 其他情况,每个位置都会朝着目标远离或者毕竟。 看看差值不是不是delta的整数倍就可以了。 如果是的话,说明可以变成x

    【代码】

    #include <bits/stdc++.h>
    #define LL long long
    using namespace std;
    
    const int N = 1e5;
    
    int n,x;
    string s;
    int pre[N+10];
    //先考虑一个s的情况
    
    int main(){
        #ifdef LOCAL_DEFINE
            freopen("E:\rush.txt","r",stdin);
        #endif // LOCAL_DEFINE
        ios::sync_with_stdio(0),cin.tie(0);
        int T;
        cin >> T;
        while (T--){
            cin >> n >> x;
            cin >> s;
            int len = s.size();
            pre[0] = 0;
            for (int i = 0;i < len;i++){
                if (s[i]=='0')
                    pre[i+1] = pre[i]+1;
                else
                    pre[i+1] = pre[i]-1;
            }
            int delta = pre[len];
            bool inf = 0;
            LL ans = 0;
            for (int i = 0;i <= len;i++){
                if (pre[i]==x) ans++;
                if (pre[i]==x && delta==0)
                    inf = 1;
                if (i!=0 && pre[i]<x && delta>0 && (x-pre[i])%delta==0) ans++;
                if (i!=0 && pre[i]>x && delta<0 && (pre[i]-x)%(-delta)==0) ans++;
            }
            if (inf) cout<<-1<<endl;else cout<<ans<<endl;
        }
        return 0;
    }
    
    
  • 相关阅读:
    at( ) & [ ]
    正则表达式 & 字符串匹配
    c++ 类 A类调用B类
    c++ 类 类指针&new对象
    重载赋值运算符
    拷贝构造函数 & 拷贝初始化
    链式表达式
    Indirect modification of overloaded element of cmfpaginatorBootstrap has no effect
    chmod(): Operation not permitted
    canvas
  • 原文地址:https://www.cnblogs.com/AWCXV/p/12242091.html
Copyright © 2011-2022 走看看