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

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

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编程语言中的解决方案有什么特别之处。例如,

// 爱情信件之谜
// https://www.hackerrank.com/challenges/the-love-letter-mystery

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

// palindrome返回将单词转换为回文所需的最小操作次数。
// 单词是一组小写ASCII字母。
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, "读取输入:", err)
			t = 0
		}
		for ; t > 0 && scanner.Scan(); t-- {
			fmt.Println(palindrome(scanner.Text()))
		}
		if t > 0 {
			fmt.Fprintln(os.Stderr, "读取输入:", io.ErrUnexpectedEOF)
		}
	}
	if err := scanner.Err(); err != nil {
		fmt.Fprintln(os.Stderr, "读取输入:", err)
	}
}

输入:

3
abc
abcba
abcd

输出:

2
0
4

输入:

3
cba
cbabc
dcba

输出:

2
0
4
英文:

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 (
	&quot;bufio&quot;
	&quot;fmt&quot;
	&quot;io&quot;
	&quot;os&quot;
	&quot;strconv&quot;
)

// 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 &lt; j; i, j = i+1, j-1 {
		n := int(word[i]) - int(word[j])
		if n &lt; 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, &quot;reading input:&quot;, err)
			t = 0
		}
		for ; t &gt; 0 &amp;&amp; scanner.Scan(); t-- {
			fmt.Println(palindrome(scanner.Text()))
		}
		if t &gt; 0 {
			fmt.Fprintln(os.Stderr, &quot;reading input:&quot;, io.ErrUnexpectedEOF)
		}
	}
	if err := scanner.Err(); err != nil {
		fmt.Fprintln(os.Stderr, &quot;reading input:&quot;, err)
	}
}

Input:

3
abc
abcba
abcd

Output:

2
0
4

Input:

3
cba
cbabc
dcba

Output:

2
0
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:

确定