String Transmission Discussions | Algorithms | HackerRank

Sort by

recency

|

28 Discussions

|

  • + 0 comments

    This problem should be a DP problem not bit Manipulation.

  • + 0 comments

    Here is String Transmission problem solution in Python Java C++ and c programming - https://programs.programmingoneonone.com/2021/07/hackerrank-string-transmission-problem-solution.html

  • + 0 comments

    /python

    T = int(input()) M = 1000000007

    from math import factorial, sqrt

    def nck(n, k): res = 0

    for i in range(k+1):
        res += factorial(n)//(factorial(i)*factorial(n-i))
    return res
    

    def divisors(n): d1 = [1] d2 = [] for i in range(2, int(sqrt(n)) + 1): if n % i == 0: d1.append(i) if i*i != n: d2.append(n//i)
    d1.extend(d2[::-1]) return d1

    for _ in range(T):

    N, K = [int(x) for x in input().split()]
    S = input()
    if N == 1:
        print(N+K)
        continue
    
    total = nck(N, K)
    div = divisors(N)
    dp = [[0]*(N+K+1) for i in range(len(div))]
    is_periodic = False
    
    for i, d in enumerate(div):
        dp[i][0] = 1
        for offset in range(d):
            zeros = 0
    
            for j in range(offset, N, d):
                if S[j] == "0":
                    zeros += 1
            ones = N//d - zeros  
    
            prev = list(dp[i])           
            dp[i][:] = [0]*(N+K+1)
    
            for k in range(K+1):
                if prev[k]:
                    dp[i][k + zeros] += prev[k]
                    dp[i][k + ones] += prev[k]
    
        if dp[i][0]:
            is_periodic = True
    
        for i2 in range(i):                
            d2 = div[i2]            
            if d % d2 == 0:
                for k in range(K+1):
                    dp[i][k] -= dp[i2][k]
    
        for k in range(1, K+1):
            total -= dp[i][k]
    
    print((total-is_periodic) % M)
    
  • + 0 comments

    for those who didnt unsderstood the question

    in the input we are given with length(n) and number of bits we can flip in this binary representation (k)...

    now for a particular test case we need to find the count such that after flipping the binary digit; the resultant binary digit is not periodic.

    eg first test case ...... 001 and k = 1

    now flip the 0 with 1 (flip the first digit) we get 101 ...this is aperiodic so count ++; (google definition of aperiodic string) now flip the second digit and youll get 011 ....aperiodic...count++;

    remember you can only flip the <=k digits in given binary representation

    if k == 3; you need to flip from 1 <= k <= 3......and check the aperiodics

  • + 0 comments

    C++ Solution

    #include <algorithm>
    #include <cstdio>
    using namespace std;
    
    typedef long long ll;
    #define FOR(i, a, b) for (int i = (a); i < (b); i++)
    #define REP(i, n) for (int i = 0; i < (n); i++)
    #define ROF(i, a, b) for (int i = (b); --i >= (a); )
    
    int ri()
    {
      int x;
      scanf("%d", &x);
      return x;
    }
    
    const int N = 1001, MOD = 1000000007;
    char a[N];
    int binom[N][N], dp[N];
    int pr[168], mu[N], np = 0;
    bool sieved[N];
      
    void getPrimes()
    {
      mu[1] = 1;
      for (int i = 2; i < N; i++) {
        if (! sieved[i]) {
          pr[np++] = i;
          mu[i] = -1;
        }
        for (int j = 0; j < np && i*pr[j] < N; j++) {
          sieved[i*pr[j]] = true;
          if (i%pr[j] == 0) {
            mu[i*pr[j]] = 0;
            break;
          }
          mu[i*pr[j]] = - mu[i];
        }
      }
    }
    
    int main()
    {
      getPrimes();
      REP(i, N+1) {
        binom[i][0] = binom[i][i] = 1;
        FOR(j, 1, i)
          binom[i][j] = (binom[i-1][j-1]+binom[i-1][j])%MOD;
      }
      for (int cc = ri(); cc--; ) {
        int n = ri(), k = ri(), ans = 0;
        scanf("%s", a);
        FOR(i, 2, n+1)
          if (n%i == 0 && mu[i]) {
            int m = n/i;
            fill_n(dp, k+1, 0);
            dp[0] = 1;
            REP(j, m) {
              int x = 0;
              for (int g = j; g < n; g += m)
                x += a[g] == '1';
              int y = i-x;
              ROF(g, 0, k+1)
                dp[g] = ((g < x ? 0 : dp[g-x]) + (g < y ? 0 : dp[g-y])) % MOD;
            }
            ll sum = 0;
            REP(j, k+1)
              sum = (sum+dp[j])%MOD;
            ans = (ans+sum*mu[i])%MOD;
          }
        REP(i, k+1)
          ans = (ans+binom[n][i])%MOD;
        printf("%d\n", (ans+MOD)%MOD);
      }
    }