Pagini recente » Cod sursa (job #2991020) | Cod sursa (job #2922458) | Cod sursa (job #2570440) | Cod sursa (job #1122935) | Cod sursa (job #635726)
Cod sursa(job #635726)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define IN_PATH "palm.in"
#define OUT_PATH "palm.out"
#define SIZE 501
int read_input (char* text) {
FILE *file;
file = fopen(IN_PATH, "r+");
if (fgets(text, SIZE, file)==NULL)
return -1;
fclose(file);
return 0;
}
int write_output (int solution) {
FILE *file;
file = fopen(OUT_PATH, "w+");
fprintf(file, "%d", solution);
fclose(file);
return 0;
}
void get_increasing (char* text, char* current, int *position) {
int i,j=0;
for(i=*position;i<strlen(text);i++) {
current[j++] = text[i];
if(text[i]>text[i+1])
break;
}
*position = i+1;
}
void palm() {
char *text = (char*)malloc(SIZE*sizeof(char));
char *current = (char*)malloc(SIZE*sizeof(char));
int position = 0, index=0, length=0, max_length=-1;
memset(text,'\0',SIZE);
memset(current,'\0',SIZE);
if(read_input(text) < 0)
return;
while(position<strlen(text)) {
get_increasing(text, current, &position);
index = strlen(current)-2;
length = index;
if(index>=0) {
while (position<strlen(text) && index>=0) {
if(current[index] != text[position])
break;
index--;position++;
}
}
length = (length-index)*2+1;
if(max_length<length)
max_length = length;
memset(current,'\0',SIZE);
}
write_output(max_length);
free(text);
free(current);
}
int main () {
palm();
return 0;
}