TOPIC

Wrong Answer (100%) PQ? Funciona normal

Abner asked 1 year ago

#include <cstdio>
#include <string.h>
using namespace std;
int main() {
    int c, tam, tam2, taminv , cont = 0, cont1;
    char n1[1000], n2[1000], auxinv[1000], aux[1000];
    scanf("%d", &c);
    for (int casos = 0; casos<c;casos++) {
        scanf("%s", &n1);
        fflush(stdin);
        scanf("%s", &n2);
        fflush(stdin);
        cont = 0;
        cont1 = tam = strlen(n1);
        tam2 = strlen(n2);
        if (tam >= tam2) {
            for (int i = 0;i < tam2;i++) {
                auxinv[cont] = n1[cont1 - 1];
                cont++;
                cont1--;
            }
            cont = 0;
            taminv = cont1 = strlen(auxinv);
            for (int x = 0;x < taminv;x++) {
                aux[cont] = auxinv[cont1 - 1];
                cont1--;
                cont++;
            }
        }
        if (strcmp(n1,n2) == 0) {
                printf("encaixa\n");
            } else {
                if (strcmp(aux, n2) == 0) {
                    printf("encaixa\n");
                }
                else {
                    printf("nao encaixa\n");    
                }
            }
        memset(auxinv, 0, 1000);
        memset(aux, 0, 1000);
    }
    return 0;
}

Remember not post solutions. Your post may be reviewed by our moderators.

  • feodorv replied 1 year ago

        char n1[1000], n2[1000], auxinv[1000], aux[1000];

    Why so small buffer sizes? You did not left the room for '\0' for example. Is it so difficult to be more generous:

        char n1[1024], n2[1024], auxinv[1024], aux[1024];
            if (tam >= tam2) {

    What if it's false? You did not init aux for such case but later you used it.

    Too many code for such a simple problem. You can use strcmp or memcmp functions to solve this problem.