Участник:Taranov srg/Longest Chunked Palindrome Decomposition

Материал из DISCOPAL
Перейти к: навигация, поиск

https://leetcode.com/problems/longest-chunked-palindrome-decomposition

C

int longestDecomposition(char * text){
 
	char chr1, chr2;
	int s_l = strlen(text);
	if(s_l == 1){
		return 1;
	}
 
	char first_start = text[0];
	for(int j=s_l-1; j>(s_l-1)/2; j--){
 
		char second_start = text[j];
 
		if(first_start == second_start){
			for(int k=0; k<s_l-j; ++k){
				chr1 = text[k];
				chr2 = text[j+k];
 
				if(chr1 != chr2){
					break;
				}
 
				if((chr1 == chr2) && ((k+j+1) == s_l)){
					// char* sub_text = memcpy(text, &text[k+1], s_l-2*(k+1)+1);
					// sub_text[s_l-2*(k+1)+1] = '\0';
					text = &text[k+1];
					text[s_l-2*(k+1)] = '\0';
					// printf("%s\n", text);
					if(s_l-2*(k+1) == 0){
						return 1  + longestDecomposition(text);
					}else{
						return 2 + longestDecomposition(text);
					}
				}
			}
		}
	};
	return 1;
};