Aiuto Caesar Cypher

Salve, sono giorni che sto cercando di risolvere Caesar Cypher ma non riesco a fare più di 5 punti, eppure l’algoritmo mi sembra giusto, ho fatto delle prove da solo e mi da sempre il risultato corretto, ma sulla piattaforma supera solo un test e gli altri o mi dà output non corretto oppure supero il tempo limite, qualcuno potrebbe aiutarmi?
Grazie in anticipo:grin:

Salve a te,
potresti postare il codice?

Ecco il codice:

#include <fstream>
#include <iostream>
#include <string>
#include <cmath>

#define MAXN 10000

using namespace std;


int decipher(int N, int D, string str[])
{

   int gruppi[N];
   int tmp[D];

   for(int i=0; i<N; i++)
   {
       gruppi[i]=0;
   }

   for(int i=0; i<N-1; i++)
   {
        string str1=str[i];

        for(int j=i+1; j<N; j++)
        {
            string str2=str[j];
            for(int h=0; h<D; h++)
            {
                tmp[h]=abs(str1[h]-str2[h]);
            }

            bool uguale=true;

            for(int z=0; z<D && uguale==true; z++)
            {
                if(tmp[0]!=tmp[z])
                {
                    uguale=false;
                    break;
                }
            }
            if(uguale==true)
            {
                gruppi[i]++;
            }

   }
   }

   int max=0;

   for(int i=0; i<D; i++)
   {
        if(max<gruppi[i])
        {
            max=gruppi[i];
        }
   }

   return max+1;

}



int main() {
    unsigned int N, D, i;

    ifstream in("input.txt");
    ofstream out("output.txt");

    in >> N;
    in >> D;

    string str[N];

    for(i=0; i<N; i++)
    {

        in >> str[i];
    }

    out << decipher(N,D,str);
    return 0;
}

Partendo dal fatto che comunque il tuo programma è troppo lento per i test case più grandi(e quindi c’è un modo più efficiente per farlo).
Il problema sta qui(secondo me): tmp[h]=abs(str1[h]-str2[h]);
Prova con un testcase del tipo:
2 2
ab
za

i primi due test case corrispondono a quelli di esempio? A me non torna neanche il primo. Forse sbaglio (anche) l’input.

Prova a postare il codice :slight_smile:

1 Mi Piace

L’ultima versione è questa:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#define MAXN 10000
int N, D, i;
char strings[MAXN][1001];
int provati[MAXN];
int quanti[MAXN];
int max;
int valmax(){
	int maxval=quanti[0];
	for(int i=1;i<N;i++)
		if(maxval<quanti[i])
			maxval=quanti[i];
	return maxval;
}

int decipher() {
	int i=0,j;
	while(i<max-1){
		if(!provati[i]){
			quanti[i]=1;
			provati[i]=1;
			for(j=i+1;j<max;j++){
				if(!provati[j]){
					if(!strcmp(strings[i],strings[j])){
						provati[j]=1;
						quanti[i]++;
					}
				}
			}
		}
		i++;
		while(max>0 && provati[max-1])
			max--;
	}
    // insert your code here
    return valmax();
}


int main() {
    freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	char c;
	int j;
    assert(2 == scanf("%d %d", &N, &D));
	max=N;
	memset(provati,0,N*sizeof(int));
	memset(quanti,0,N*sizeof(int));
	c=getchar();
    //strings = (char**)malloc(N*sizeof(char*));
    for(i=0; i<N; i++) {
        //strings[i] = (char*)malloc((D+1)*sizeof(char));
        assert(1 == scanf("%s", strings[i]));
    }
	for(i=0;i<N;i++){
		for(j=D-1;j>=0;j--){
			strings[i][j]=strings[i][j]-strings[i][0]+100;
		}
	}

    printf("%d\n", decipher());
    return 0;
}

grazie

dovrei aver trascurato il fatto che c’è di mezzo la matematica modulo 26: az e ba vanno d’accordo ma non per il mio codice

Ho sistemato la matematica modulo 26 adesso le cose vanno decisamente meglio, ora è solo troppo lento.
Grazie comunque