Skip to content

Instantly share code, notes, and snippets.

@bergolho
Created April 27, 2017 17:34
Show Gist options
  • Save bergolho/5d90eb754dcd9452530cf17eab79cbcb to your computer and use it in GitHub Desktop.
Save bergolho/5d90eb754dcd9452530cf17eab79cbcb to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <set>
#include <cstring>
using namespace std;
const int MAX_BYTES = 10005; // Tamanho maximo de um presente
char fac[27][27]; // Configuracao atual da fabrica
int lines[27]; // Vetor que armazena as trocas entre os gnomos
char alfa[27]; // Alfabeto
set<string> listPresent; // Lista da resposta
// DEBUG
void printFactory ()
{
for (int i = 1; i < 27; i++)
{
for (int j = 1; j < 27; j++)
printf("%c ",fac[i][j]);
printf("\n");
}
}
// DEBUG
void printConfiguration ()
{
printf("Lines\n");
for (int i = 1; i < 27; i++)
printf("%d ",lines[i]);
printf("\n\n");
}
void buildAlfabet ()
{
for (int i = 1; i < 27; i++)
alfa[i] = 'A' + i - 1;
}
void buildFactory ()
{
for (int i = 1; i < 27; i++)
{
memcpy(fac[i],alfa,sizeof(char)*27);
lines[i] = i;
}
}
void changeLine (int l)
{
int tmp = lines[l];
for (int i = l; i < 26; i++)
lines[i] = lines[i+1];
lines[26] = tmp;
}
void changeColumn (int c, char letter)
{
for (int i = c; i < 26; i++)
{
for (int j = 1; j < 27; j++)
fac[j][i] = fac[j][i+1];
}
for (int i = 1; i < 27; i++)
fac[i][26] = letter;
}
char processToken (char *token)
{
char c;
int i = atoi(token);
if (i == 27)
c = ' ';
else
{
c = fac[i][lines[i]];
changeColumn(lines[i],fac[i][lines[i]]);
changeLine(i);
}
return c;
}
int main ()
{
int N, K;
char *token;
char *present = (char*)malloc(sizeof(char)*MAX_BYTES);
buildAlfabet();
K = 1;
while (scanf("%d ",&N) != EOF)
{
if (K != 1) printf("\n\n");
// Construir configuracao da fabrica original
buildFactory();
printf("LISTA #%d:\n",K);
// Ler linhas de producao
for (int i = 0; i < N; i++)
{
string pres_name;
fgets(present,MAX_BYTES,stdin);
// Tokenizar e processar cada numero
token = strtok(present," ");
while (token != NULL)
{
// Ir montando a string do presente
char c = processToken(token);
pres_name += c;
token = strtok(NULL," ");
}
listPresent.insert(pres_name);
}
// Imprimir a saida no formato
int i;
set<string>::iterator it;
int n = (int)listPresent.size();
for (i = 0, it = listPresent.begin(); i < n-1; ++it, i++)
printf("%s\n",it->c_str());
printf("%s",it->c_str());
listPresent.clear();
K++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment