Calcule la puntuación para la string binaria dada

Dada una string binaria str . Para n-1s contiguos, la puntuación se actualiza como puntuación = puntuación + n 2 y para n-0s contiguos , la puntuación se actualiza como puntuación = puntuación – n 2 . La tarea es encontrar la puntuación de la string binaria completa.
Ejemplos: 
 

Entrada: str = 11011 
Salida:
puntaje(“11”) – puntaje(“0”) + puntaje(“11”) = 2 2 – 1 2 + 2 2 = 7
Entrada: str = 1100011 
Salida: -1 
 

Enfoque: para resolver el problema, itere sobre la string dada y calcule el número de 1 y 0 contiguos . Para cada porción contigua de n 1 , agregue n 2 a la puntuación actual y, de manera similar, para cada porción contigua de n 0, reste n 2 de la puntuación actual.
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the score for
// the given binary string
int calcScore(string str)
{
    int score = 0;
    int len = str.length();
 
    // Traverse through string character
    for (int i = 0; i < len;) {
 
        // Initialize current chunk's size
        int chunkSize = 1;
 
        // Get current character
        char currentChar = str[i++];
 
        // Calculate total chunk size
        // of same characters
        while (i < len && str[i] == currentChar) {
            chunkSize++;
            i++;
        }
 
        // Add/subtract pow(chunkSize, 2)
        // depending upon character
        if (currentChar == '1')
            score += pow(chunkSize, 2);
        else
            score -= pow(chunkSize, 2);
    }
 
    // Return the score
    return score;
}
 
// Driver code
int main()
{
    string str = "11011";
    cout << calcScore(str);
 
    return 0;
}

Java

// Java implementation of the approach
class GFG
{
     
    // Function to return the score for
    // the given binary string
    public static int calcScore(String str)
    {
        int score = 0;
        int len = str.length();
 
        // Traverse through string character
        for (int i = 0; i < len;)
        {
 
            // Initialize current chunk's size
            int chunkSize = 1;
 
            // Get current character
            char currentChar = str.charAt(i++);
 
            // Calculate total chunk size
            // of same characters
            while (i < len && str.charAt(i) == currentChar)
            {
                chunkSize++;
                i++;
            }
 
            // Add/subtract pow(chunkSize, 2)
            // depending upon character
            if (currentChar == '1')
                score += Math.pow(chunkSize, 2);
            else
                score -= Math.pow(chunkSize, 2);
        }
         
        // Return the score
        return score;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String str = "11011";
        System.out.println(calcScore(str));
    }
}
 
// This code is contributed by Naman_Garg

Python3

# Python 3 implementation of the approach
 
# Function to return the score for
# the given binary string
def calcScore(str):
    score = 0
    len1 = len(str)
 
    # Traverse through string character
    i = 0
    while(i < len1):
         
        # Initialize current chunk's size
        chunkSize = 1
 
        # Get current character
        currentChar = str[i]
        i += 1
 
        # Calculate total chunk size
        # of same characters
        while (i < len1 and str[i] == currentChar):
            chunkSize += 1
            i += 1
 
        # Add/subtract pow(chunkSize, 2)
        # depending upon character
        if (currentChar == '1'):
            score += pow(chunkSize, 2)
        else:
            score -= pow(chunkSize, 2)
 
    # Return the score
    return score
 
# Driver code
if __name__ == '__main__':
    str = "11011"
    print(calcScore(str))
 
# This code is contributed by
# Surendra_Gangwar

C#

// C# implementation of the approach
using System;
     
class GFG
{
     
    // Function to return the score for
    // the given binary string
    public static int calcScore(String str)
    {
        int score = 0;
        int len = str.Length;
 
        // Traverse through string character
        for (int i = 0; i < len;)
        {
 
            // Initialize current chunk's size
            int chunkSize = 1;
 
            // Get current character
            char currentChar = str[i++];
 
            // Calculate total chunk size
            // of same characters
            while (i < len && str[i] == currentChar)
            {
                chunkSize++;
                i++;
            }
 
            // Add/subtract pow(chunkSize, 2)
            // depending upon character
            if (currentChar == '1')
                score += (int)Math.Pow(chunkSize, 2);
            else
                score -= (int)Math.Pow(chunkSize, 2);
        }
         
        // Return the score
        return score;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        String str = "11011";
        Console.WriteLine(calcScore(str));
    }
}
 
// This code contributed by Rajput-Ji

PHP

<?php
// Php implementation of the approach
 
// Function to return the score for
// the given binary string
function calcScore($str)
{
    $score = 0;
    $len = strlen($str);
 
    // Traverse through string character
    for ($i = 0; $i < $len😉
    {
 
        // Initialize current chunk's size
        $chunkSize = 1;
 
        // Get current character
        $currentChar = $str[$i++];
 
        // Calculate total chunk size
        // of same characters
        while ($i < $len && $str[$i] == $currentChar)
        {
            $chunkSize++;
            $i++;
        }
 
        // Add/subtract pow(chunkSize, 2)
        // depending upon character
        if ($currentChar == '1')
            $score += pow($chunkSize, 2);
        else
            $score -= pow($chunkSize, 2);
    }
 
    // Return the score
    return $score;
}
 
    // Driver code
    $str = "11011";
    echo calcScore($str);
     
    // This code is contributed by AnkitRai01
?>

Javascript

<script>
 
// Javascript implementation of the approach
 
// Function to return the score for
// the given binary string
function calcScore(str)
{
    var score = 0;
    var len = str.length;
 
    // Traverse through string character
    for (var i = 0; i < len;) {
 
        // Initialize current chunk's size
        var chunkSize = 1;
 
        // Get current character
        var currentChar = str[i++];
 
        // Calculate total chunk size
        // of same characters
        while (i < len && str[i] == currentChar) {
            chunkSize++;
            i++;
        }
 
        // Add/subtract pow(chunkSize, 2)
        // depending upon character
        if (currentChar == '1')
            score += Math.pow(chunkSize, 2);
        else
            score -= Math.pow(chunkSize, 2);
    }
 
    // Return the score
    return score;
}
 
// Driver code
var str = "11011";
document.write( calcScore(str));
 
 
</script>
Producción: 

7

 

Publicación traducida automáticamente

Artículo escrito por Shivam.Pradhan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *