Created
December 12, 2014 01:26
-
-
Save hyunjun/7a507bd0139c8a84cd9a to your computer and use it in GitHub Desktop.
number of reduction to make palindrome
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <math.h> | |
#define MAX_STR_LEN 10000 | |
int num_of_reduction(const char const * s) { | |
if ( s == NULL || 0 == strlen(s) || MAX_STR_LEN < strlen(s) ) | |
return 0; | |
int len = strlen(s), cnt = 0, l = 0, r = len - 1; | |
while ( l < r ) { | |
int l_num = *(s + l); | |
int r_num = *(s + r); | |
if ( l_num != r_num ) { | |
cnt += abs(l_num - r_num); | |
} | |
++l; | |
--r; | |
} | |
return cnt; | |
} | |
int main() { | |
int num_of_test_case = 0; | |
fscanf(stdin, "%d", &num_of_test_case); | |
int* test_cases = (int*) malloc(sizeof(int) * num_of_test_case); | |
int i = 0; | |
for ( i = 0; i < num_of_test_case; ++i ) { | |
char s[MAX_STR_LEN + 1]; | |
memset(s, '\0', MAX_STR_LEN + 1); | |
fscanf(stdin, "%s", s); | |
printf("%d\n", num_of_reduction(s)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# python2 | |
import sys | |
def num_of_reduction(s): | |
cnt, m, r = 0, len(s) / 2, len(s) - 1 | |
for l in range(m): | |
l_num, r_num = ord(s[l]), ord(s[r]) | |
bigger, smaller = max(l_num, r_num), min(l_num, r_num) | |
if bigger != smaller: | |
cnt += bigger - smaller | |
r -= 1 | |
return cnt | |
if __name__ == '__main__': | |
inp = [] | |
for line in sys.stdin: | |
inp.append(line.strip()) | |
for i in inp[1:]: | |
print num_of_reduction(i) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# python3 | |
import sys | |
def num_of_reduction(s): | |
cnt, m, r = 0, int(len(s) / 2), len(s) - 1 | |
for l in range(m): | |
l_num, r_num = ord(s[l]), ord(s[r]) | |
bigger, smaller = max(l_num, r_num), min(l_num, r_num) | |
if bigger != smaller: | |
cnt += bigger - smaller | |
r -= 1 | |
return cnt | |
if __name__ == '__main__': | |
inp = [] | |
for line in sys.stdin: | |
inp.append(line.strip()) | |
for i in inp[1:]: | |
print(num_of_reduction(i)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Solution { | |
public static int numOfReduction(final String s) { | |
int cnt = 0; | |
int l = 0; | |
int r = s.length() - 1; | |
while ( l < r ) { | |
int lNum = s.charAt(l); | |
int rNum = s.charAt(r); | |
if ( lNum != rNum ) { | |
cnt += Math.abs(lNum - rNum); | |
} | |
++l; | |
--r; | |
} | |
return cnt; | |
} | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int numberOfLines = Integer.parseInt(sc.nextLine()); | |
long start = 0; | |
for (int i = 1; i<= numberOfLines;i++){ | |
System.out.println(numOfReduction(sc.nextLine())); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.hackerrank.com/challenges/the-love-letter-mystery | |
object Solution { | |
def numOfReduction(s: String): Int = { | |
var cnt: Int = 0 | |
var l: Int = 0 | |
var r: Int = s.length - 1 | |
while ( l < r ) { | |
cnt += Math.abs(s(l) - s(r)) | |
l += 1; | |
r -= 1; | |
} | |
cnt | |
} | |
def main(args: Array[String]) { | |
val inp = io.Source.stdin.getLines.drop(1) | |
for (i <- inp) println(numOfReduction(i)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment