Solve with Ruby, Python and Java AtCoder ARC104 B Cumulative sum

Introduction

This theme

AtCoder Regular Contest B - DNA Sequence Difficulty: 410

This theme, cumulative sum

This is a cumulative sum problem, but this time we need four cumulative sums of ʻA C G`` T`. Ruby

ruby.rb


n, s = gets.chomp.split
n = n.to_i
a = [0]
c = [0]
g = [0]
t = [0]
n.times do |i|
  a << a[-1]
  c << c[-1]
  g << g[-1]
  t << t[-1]
  if s[i] == 'A'
    a[i + 1] += 1
  elsif s[i] == 'C'
    c[i + 1] += 1
  elsif s[i] == 'G'
    g[i + 1] += 1
  elsif s[i] == 'T'
    t[i + 1] += 1
  end
end
cnt = 0
n.times do |i|
  at = a[i] - t[i]
  cg = c[i] - g[i]
  (i.next).upto(n) do |j|
    cnt += 1 if a[j] - t[j] == at && c[j] - g[j] == cg
  end
end
puts cnt

I have reflected the idea of @scivola in the comment section.

ruiseki.rb


  a << a[-1]
  c << c[-1]
  g << g[-1]
  t << t[-1]

When creating a cumulative array, you can also prepare the required number of elements first. This time, I called the final element with [-1] and added it.

hantei.rb


  at = a[i] - t[i]
  cg = c[i] - g[i]
  (i.next).upto(n) do |j|
    cnt += 1 if a[j] - t[j] == at && c[j] - g[j] == cg
  end

If the number of elements of ʻA C G`` Tin the interval is the same, it can be judged ascomplementary`. Python

pypy.py


from sys import stdin

def main():
    input = stdin.readline
    n, s = input().split()
    n = int(n)
    s = 'N' + s + 'N'
    cnt = 0
    for i in range(1, n + 2):
        a = 0
        c = 0
        g = 0
        t = 0
        j = i + 1
        while True:
            if s[i] == 'A':
                a += 1
            elif s[i] == 'C':
                c += 1
            elif s[i] == 'G':
                g += 1
            elif s[i] == 'T':
                t += 1
            else:
                break
            if s[j] == 'A':
                a += 1
            elif s[j] == 'C':
                c += 1
            elif s[j] == 'G':
                g += 1
            elif s[j] == 'T':
                t += 1
            else:
                break
            if a == t and c == g:
                cnt += 1
            i -= 1
            j += 1
    print(cnt)
main()

pypy is another solution. With this solution, it becomes TLE in Python and Ruby.

banpei.py


    s = 'N' + s + 'N'

There are sentinels before and after the string. It is used for the condition of break with while.

hiroge.py


            i -= 1
            j += 1

We expand the range at each character position and check the number Java

java.java


import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        String s = "N" + sc.next() + "N";
        sc.close();

        int cnt = 0;
        for (int k = 1; k < n+2; k++) {
            int i = k;
            int j = k + 1;
            int gn = 0;
            int cn = 0;
            int tn = 0;
            int an = 0;
            while (true) {
                if (s.charAt(i) == 'A') {
                    an++;
                } else if (s.charAt(i) == 'C') {
                    cn++;
                } else if (s.charAt(i) == 'G') {
                    gn++;
                } else if (s.charAt(i) == 'T') {
                    tn++;
                } else {
                    break;
                }
                if (s.charAt(j) == 'A') {
                    an++;
                } else if (s.charAt(j) == 'C') {
                    cn++;
                } else if (s.charAt(j) == 'G') {
                    gn++;
                } else if (s.charAt(j) == 'T') {
                    tn++;
                } else {
                    break;
                }
                if (an == tn && cn == gn) {
                    cnt++;
                }
                i--;
                j++;
            }
        }
        System.out.println(cnt);
    }
}

Java is the same solution as Pypy. I have reflected the idea of @ c-yan in the comment section.

i.java


        for (int k = 1; k < n+2; k++) {
            int i = k;

Unlike Python and Ruby, if you assign to a loop variable in a block, it will affect the next loop variable, so you assign it to another variable.

Ruby Pypy Java
Code length(Byte) 482 920 1436
Execution time(ms) 1011 336 335
memory(KB) 14676 69312 39456

Summary

Recommended Posts

Solve with Ruby, Python and Java AtCoder ARC104 B Cumulative sum
Solving with Ruby, Perl, Java, and Python AtCoder ARC 098 C Cumulative sum
Solve with Ruby and Python AtCoder ABC084 D Cumulative sum of prime numbers
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 B
Solving with Ruby, Perl, Java and Python AtCoder ABC 107 B String Manipulation
Solve with Ruby, Perl, Java and Python AtCoder ABC 047 C Regular Expression
Sorting AtCoder ARC 086 C hashes to solve in Ruby, Perl, Java and Python
Solving with Ruby and Python AtCoder ARC 059 C Least Squares
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 A
Solving with Ruby and Python AtCoder ARC067 C Prime Factorization
Solving with Ruby, Python and numpy AtCoder ABC054 B Matrix operation
Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial
AtCoder JSC2019 Qual B to solve with Ruby and Python Inverse element of arithmetic progression
Solve AtCoder 167 with python
AtCoder ABC130 D Cumulative Sum Binary Search Solved by Ruby and Python
Solving with Ruby, Perl, Java and Python AtCoder CADDi 2018 C Prime Factorization
Solving with Ruby, Perl, Java and Python AtCoder ABC 165 D Floor function
Solving with Ruby, Perl, Java and Python AtCoder ABC 131 D Array Sorting
AtCoder ABC172 C Cumulative Sum Binary Search Solved by Ruby and Python
Solve AtCoder ABC166 with python
AtCoder ARC080 D simulation solved in Ruby and Python
Solving with Ruby and Python AtCoder CODE FESTIVAL 2016 qual C B Priority queue
Solving in Ruby, Perl, Java, and Python AtCoder ARC 066 C Iterative Squares Hash
Solve AtCoder ABC 186 with Python
Solving with Ruby, Perl, Java and Python AtCoder diverta 2019 Programming Contest C String Manipulation
Solving with Ruby and Python AtCoder ABC178 D Dynamic programming
Solving with Ruby and Python AtCoder ABC151 D Breadth-first search
Solving with Ruby and Python AtCoder AISING2020 D Iterative Squares
Solving with Ruby and Python AtCoder ABC011 C Dynamic programming
Solving with Ruby and Python AtCoder ABC153 E Dynamic programming
Solving with Ruby and Python AtCoder ABC138 D Adjacency list
Scraping with Node, Ruby and Python
Solve AtCoder Problems Recommendation with python (20200517-0523)
Solving with Ruby, Python and networkx AtCoder ABC168 D Adjacency list
Solve AtCoder ABC168 with python (A ~ D)
Encrypt with Ruby (Rails) and decrypt with Python
Easy web scraping with Python and Ruby
Solving with Ruby and Python AtCoder ABC057 C Prime Factorization Bit Search
AtCoder Beginner Contest 170 B Problem "Crane and Turtle" Explanation (Python3, C ++, Java)
MessagePack-Try to link Java and Python with RPC
Solve "AtCoder version! Ant book (beginner)" with Python!
Benchmark for C, Java and Python with prime factorization
Ruby, Python and map
Solve simultaneous ordinary differential equations with Python and SymPy.
Python and Ruby split
Investigate Java and python data exchange with Apache Arrow
Solve Atcoder ABC176 (A, B, C, E) in Python
Solve POJ 2386 with python
Comparison of CoffeeScript with JavaScript, Python and Ruby grammar
Version control of Node, Ruby and Python with anyenv
[Python] Cumulative sum ABC179D
AtCoder Beginner Contest 174 B Problem "Distance" Explanation (C ++, Python, Java)
AtCoder Beginner Contest 177 B Problem "Substring" Explanation (Python3, C ++, Java)
[AtCoder explanation] Control ABC180 A, B, C problems with Python!
[AtCoder explanation] Control ABC188 A, B, C problems with Python!
Poisson distribution and Poisson cumulative distribution plot via sqlite in Python and Java
Solve the spiral book (algorithm and data structure) with python!
AtCoder Beginner Contest 169 B Problem "Multiplication 2" Explanation (Python3, C ++, Java)
Solve AtCoder Problems Boot camp for Beginners (Medium 100) with python
AtCoder ABC168 A case expression solved in Ruby and Python
[AtCoder explanation] Control ABC164 A, B, C problems with Python!