dongzhong8691 2014-06-21 18:23
浏览 28
已采纳

谁能解决这个编程难题? [关闭]

Can anybody solve the following question using the programming language Go?

James got hold of a love letter that his friend Harry has written for his girlfriend. Being the prankster that James is, he decides to meddle with it. He changes all the words in the letter into palindromes.

While modifying the letters of the word, he follows 2 rules:

  1. He always reduces the value of a letter, e.g. he changes 'd' to 'c', but he does not change 'c' to 'd'.

  2. If he has to repeatedly reduce the value of a letter, he can do it until the letter becomes 'a'. Once a letter has been changed to 'a', it can no longer be changed.

Each reduction in the value of any letter is counted as a single operation. Find the minimum number of operations he carries out to convert a given string into a palindrome.

Input Format

The first line contains an integer T, i.e., the number of test cases. The next T lines will contain a string each.

Output Format

A single line containing the number of minimum operations corresponding to each test case.

Constraints

1 ≤ T ≤ 10 1 ≤ length of string ≤ 104 All characters are lower cased.

Sample Input

#00 3 abc abcba abcd

Sample Output

#00 2 0 4

Explanation

For the first test case, ab*c* -> ab*b* -> ab*a*. For the second test case, abcba is a palindromic string. For the third test case, abc*d* -> abc*c* -> abc*b* -> abc*a* = ab*c*a -> ab*b*a.

The puzzle is taken from HackerRank.com I am a golang newbie and couldn't solve the puzzle using the language.

  • 写回答

1条回答 默认 最新

  • douhe4608 2014-06-22 00:10
    关注

    I don't see anything particularly special about a solution in the Go programming language. For example,

    // The Love-Letter Mystery
    // https://www.hackerrank.com/challenges/the-love-letter-mystery
    
    package main
    
    import (
        "bufio"
        "fmt"
        "io"
        "os"
        "strconv"
    )
    
    // palindrome returns the minimum number of operations carried out
    // to convert a word into a palindrome.
    // A word is a set of lower-case ASCII letters.
    func palindrome(word string) (ops int) {
        for i, j := 0, len(word)-1; i < j; i, j = i+1, j-1 {
            n := int(word[i]) - int(word[j])
            if n < 0 {
                n = -n
            }
            ops += n
        }
        return ops
    }
    
    func main() {
        scanner := bufio.NewScanner(os.Stdin)
        if scanner.Scan() {
            t, err := strconv.Atoi(scanner.Text())
            if err != nil {
                fmt.Fprintln(os.Stderr, "reading input:", err)
                t = 0
            }
            for ; t > 0 && scanner.Scan(); t-- {
                fmt.Println(palindrome(scanner.Text()))
            }
            if t > 0 {
                fmt.Fprintln(os.Stderr, "reading input:", io.ErrUnexpectedEOF)
            }
        }
        if err := scanner.Err(); err != nil {
            fmt.Fprintln(os.Stderr, "reading input:", err)
        }
    }
    

    Input:

    3
    abc
    abcba
    abcd
    

    Output:

    2
    0
    4
    

    Input:

    3
    cba
    cbabc
    dcba
    

    Output:

    2
    0
    4
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?