How to optimize this code (Time Limit Exceeded)?

1

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

#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;

    }
}
    
asked by anonymous 03.07.2018 / 05:09

1 answer

1

Use while (!cin.eof()) instead of while(N!= EOF) .

See here working on ideone.

    
03.07.2018 / 06:13