# 1257 - Array Hash

## Descrição

{% embed url="<https://www.urionlinejudge.com.br/judge/pt/problems/view/1257>" %}

## Solução

Aqui podemos entender que todas as strings passadas formam como se fosse uma matriz, onde cada linha é uma string e cada coluna é uma letra. Entretanto, não temos necessidade de fazer uma matriz completa para esse problema, se só ficarmos com a matriz em mente e entendermos que temos então linha i e coluna j, então podemos organizar direitinho os for que vamos precisar para fazer o cálculo do modo mais fácil possível, usando a [tabela ASCII](/solucoes-da-beecrowd/base-teorica/strings/tabela-ascii.md) para calcular rapidamente a posição de cada letra no alfabeto.

{% hint style="danger" %}
O código em JavaScript para este problema não está disponível no momento. :(
{% endhint %}

{% tabs %}
{% tab title="C99" %}

```c
#include <string.h>
#include <stdio.h>

int main(){
    int N, L, hash;
    char frase[51];

    scanf("%d", &N);
    for(int k = 0; k < N; ++k){
        hash = 0;
        scanf("%d\n", &L);

        for(int i = 0; i < L; ++i){
            scanf("%s\n", &frase);

            for(int j = 0; j < strlen(frase); ++j){
                hash += (frase[j] - 'A') + i + j;
            }
        }

        printf("%d\n", hash);
    }

    return 0;
}
```

{% endtab %}

{% tab title="C++17" %}

```cpp
#include <iostream>

using namespace std;

int main(){
    string frase;
    int N, L, hash;

    cin >> N;

    for(int k = 0; k < N; ++k){
        hash = 0;
        cin >> L;

        for(int i = 0; i < L; ++i){
            cin >> frase;

            for(int j = 0; j < frase.length(); ++j){
                hash += (frase[j] - 'A') + i + j;
            }
        }

        cout << hash << endl;
    }

    return 0;
}
```

{% endtab %}

{% tab title="Python 3.9" %}

```python
N = int(input())

for _ in range(N):
    valor = 0
    L = int(input())

    for i in range(L):
        frase = input()

        for j in range(len(frase)):
            valor += ord(frase[j]) - ord('A') + i + j
    
    print(valor)
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xtecna.gitbook.io/solucoes-da-beecrowd/strings/1257-array-hash.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
