Pagini recente » Cod sursa (job #2879973) | Cod sursa (job #195706) | Cod sursa (job #819452) | Cod sursa (job #450219) | Cod sursa (job #635622)
Cod sursa(job #635622)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define IN_PATH "palm.in"
#define OUT_PATH "palm.out"
#define S 501
int read_input (char* text) {
FILE *file;
file = fopen(IN_PATH, "r+");
if (fgets(text, 500, file)==NULL)
return -1;
return 0;
}
int write_output (int solution) {
FILE *file;
file = fopen(OUT_PATH, "w+");
fprintf(file, "%d", solution);
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(S*sizeof(char));
char *current = (char*)malloc(S*sizeof(char));
int position = 0, index, length, max_length=-1;
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',S);
}
write_output(max_length);
}
int main () {
palm();
}