How to optimize this code (Time Limit Exceeded)?

Asked

Viewed 1,012 times

1

This code is a possible solution to the 1211 problem in the URI Online Judge. On my pc runs normal for the test cases, but on the platform I get the message from Time Limit Exceeded. Any optimization idea?

#include <iostream>
#include <string.h>
using namespace std;

struct telefone{
    char digito[200];
};

int main(void){

    long int i = 0, N = 0;
    int soma = 0, j = 0;

        cin >> N;

    while(N!= EOF){
    struct telefone tel[N];

    for(i=0; i<N; i++){
        cin >> tel[i].digito;
    }
    int tamanho = strlen(tel[0].digito);

    for(i = 0; i < N - 1 ; i++){
        for(j=0; j<tamanho; j++){
                if (tel[i].digito[j] == tel[i+1].digito[j])
                    soma++;
                else break;
            }
    }
    cout << soma << endl;
    soma = 0;
    cin >> N;

    }
}
  • C is not C++, its code is C++ and not C, I edited the tags.

  • I don’t know what the problem is, the 1211 of the Judge URI. If you can summarize it so that we can get a better idea of what it is, it’s better to try to help. But if it helps you, by this automatic scope vector of size N to be allocated at the beginning of the block to each repetition, this is a mistake in competitive programming. Best leave this vector already allocated with its maximum value or don’t use it if you don’t need to store more than two sequences of digits at a time

  • Example of competitive programming issue that fits much better in the format of Sopt: https://answall.com/q/306086/64969

1 answer

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.