Cod sursa(job #635502)

Utilizator alexa42alex androne alexa42 Data 19 noiembrie 2011 12:21:48
Problema PalM Scor 0
Compilator c Status done
Runda .com 2011 Marime 1.19 kb
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define IN_PATH "palm.in"
#define OUT_PATH "palm.out"

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(500*sizeof(char));
	char *current = (char*)malloc(500*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',500);
	}

	write_output(max_length);
}

int main () {
	palm();
}