有人能解决这个编程挑战吗?

huangapple go评论109阅读模式
英文:

Can anybody solve this programming challenge?

问题

有人能使用Go编程语言解决以下问题吗?

James拿到了他朋友Harry为女朋友写的一封情书。作为恶作剧的James,他决定对它进行干预。他将信中的所有单词都改成了回文。

在修改单词的过程中,他遵循以下两个规则:

  1. 他总是减小字母的值,例如他将'd'改为'c',但不会将'c'改为'd'。
  1. 如果他需要重复减小字母的值,他可以一直减小直到字母变为'a'。一旦一个字母被改为'a',就不能再改变。

对于任何字母值的减小,都计为一次操作。找出他将给定字符串转换为回文所需的最小操作次数。

输入格式

第一行包含一个整数T,即测试用例的数量。接下来的T行每行包含一个字符串。

输出格式

一行,包含每个测试用例对应的最小操作次数。

约束条件

1 ≤ T ≤ 10
1 ≤ 字符串长度 ≤ 104
所有字符都是小写字母。

示例输入

#00 3 abc abcba abcd

示例输出

#00 2 0 4

解释

对于第一个测试用例,abc -> abb -> aba。对于第二个测试用例,abcba是一个回文字符串。对于第三个测试用例,abcd -> abcc -> abcb -> abca = abca -> abba。

这个谜题来自HackerRank.com。作为一个Go语言的新手,我无法使用这种语言解决这个谜题。

英文:

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, abc -> abb -> aba. For the
> second test case, abcba is a palindromic string. For the third test
> case, abcd -> abcc -> abcb -> abca = abca -> abba.

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

答案1

得分: 0

我看不出Go编程语言中的解决方案有什么特别之处。例如,

  1. // 爱情信件之谜
  2. // https://www.hackerrank.com/challenges/the-love-letter-mystery
  3. package main
  4. import (
  5. "bufio"
  6. "fmt"
  7. "io"
  8. "os"
  9. "strconv"
  10. )
  11. // palindrome返回将单词转换为回文所需的最小操作次数。
  12. // 单词是一组小写ASCII字母。
  13. func palindrome(word string) (ops int) {
  14. for i, j := 0, len(word)-1; i < j; i, j = i+1, j-1 {
  15. n := int(word[i]) - int(word[j])
  16. if n < 0 {
  17. n = -n
  18. }
  19. ops += n
  20. }
  21. return ops
  22. }
  23. func main() {
  24. scanner := bufio.NewScanner(os.Stdin)
  25. if scanner.Scan() {
  26. t, err := strconv.Atoi(scanner.Text())
  27. if err != nil {
  28. fmt.Fprintln(os.Stderr, "读取输入:", err)
  29. t = 0
  30. }
  31. for ; t > 0 && scanner.Scan(); t-- {
  32. fmt.Println(palindrome(scanner.Text()))
  33. }
  34. if t > 0 {
  35. fmt.Fprintln(os.Stderr, "读取输入:", io.ErrUnexpectedEOF)
  36. }
  37. }
  38. if err := scanner.Err(); err != nil {
  39. fmt.Fprintln(os.Stderr, "读取输入:", err)
  40. }
  41. }

输入:

  1. 3
  2. abc
  3. abcba
  4. abcd

输出:

  1. 2
  2. 0
  3. 4

输入:

  1. 3
  2. cba
  3. cbabc
  4. dcba

输出:

  1. 2
  2. 0
  3. 4
英文:

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

  1. // The Love-Letter Mystery
  2. // https://www.hackerrank.com/challenges/the-love-letter-mystery
  3. package main
  4. import (
  5. &quot;bufio&quot;
  6. &quot;fmt&quot;
  7. &quot;io&quot;
  8. &quot;os&quot;
  9. &quot;strconv&quot;
  10. )
  11. // palindrome returns the minimum number of operations carried out
  12. // to convert a word into a palindrome.
  13. // A word is a set of lower-case ASCII letters.
  14. func palindrome(word string) (ops int) {
  15. for i, j := 0, len(word)-1; i &lt; j; i, j = i+1, j-1 {
  16. n := int(word[i]) - int(word[j])
  17. if n &lt; 0 {
  18. n = -n
  19. }
  20. ops += n
  21. }
  22. return ops
  23. }
  24. func main() {
  25. scanner := bufio.NewScanner(os.Stdin)
  26. if scanner.Scan() {
  27. t, err := strconv.Atoi(scanner.Text())
  28. if err != nil {
  29. fmt.Fprintln(os.Stderr, &quot;reading input:&quot;, err)
  30. t = 0
  31. }
  32. for ; t &gt; 0 &amp;&amp; scanner.Scan(); t-- {
  33. fmt.Println(palindrome(scanner.Text()))
  34. }
  35. if t &gt; 0 {
  36. fmt.Fprintln(os.Stderr, &quot;reading input:&quot;, io.ErrUnexpectedEOF)
  37. }
  38. }
  39. if err := scanner.Err(); err != nil {
  40. fmt.Fprintln(os.Stderr, &quot;reading input:&quot;, err)
  41. }
  42. }

Input:

  1. 3
  2. abc
  3. abcba
  4. abcd

Output:

  1. 2
  2. 0
  3. 4

Input:

  1. 3
  2. cba
  3. cbabc
  4. dcba

Output:

  1. 2
  2. 0
  3. 4

huangapple
  • 本文由 发表于 2014年6月22日 02:23:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/24344538.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定