# Akureyri\*

## Question

{% embed url="<https://open.kattis.com/problems/akureyri>" %}

## Solution

{% hint style="warning" %}
**Future Improvement:** Use Hash Table Idea to improve this in the future!
{% endhint %}

### Idea

The general idea for this idea is relative easy. Basically, you just need to count how many times each unique location has appeared.

However, to implement it in C, it is a bit troublesome. I am not sure whether there will be a easier way to do that, but in my `main` function, attention should be paid on how to deal with memory allocation.

{% code lineNumbers="true" %}

```c
int main()
{
  size_t n = cs1010_read_size_t();
  char **loc_arr = calloc(n, sizeof(char *));
  long *freq = calloc(n, sizeof(long));
  if (loc_arr == NULL)
  {
    return 1;
  }
  long len = 0;
  // Update freq and loc_arr
  for (size_t i = 0; i < n; i += 1)
  {
    char *name = cs1010_read_word();
    char *loc = cs1010_read_word();
    long duplicate = find_duplicate(loc_arr, (long)n, loc);
    if (duplicate != -1)
    {
      freq[duplicate] += 1;
      free(loc);
    }
    else
    {
      loc_arr[len] = loc;
      freq[len] += 1;
      len += 1;
    }
    free(name);
  }

  // Print result
  print_res(loc_arr, freq, (long)n);

  // Free Memory
  for (long i = 0; i < n; i += 1)
  {
    free(loc_arr[i]);
  }
  free(loc_arr);
  free(freq);
}

```

{% endcode %}

Here the general idea is as follows:

1. If `loc` is not duplicate, then we only need to free `name` .
2. Otherwise, we need to free `name` and `loc` .

### Code

{% @github-files/github-code-block url="<https://github.com/mendax1234/Coding-Problems/blob/main/kattis/akureyri/akureyri.c>" %}


---

# 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://wenbo-notes.gitbook.io/coding/kattis/easy/akureyri.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.
