编程介的小学生 2019-12-03 00:12 采纳率: 0.4%
浏览 84

Ice-sugar Gourd 编写代码

Problem Description
Ice-sugar gourd, “bing tang hu lu”, is a popular snack in Beijing of China. It is made of some fruits threaded by a stick. The complicated feeling will be like a both sour and sweet ice when you taste it. You are making your mouth water, aren’t you?

I have made a huge ice-sugar gourd by two kinds of fruit, hawthorn and tangerine, in no particular order. Since I want to share it with two of my friends, Felicia and his girl friend, I need to get an equal cut of the hawthorns and tangerines. How many times will I have to cut the stick so that each of my friends gets half the hawthorns and half the tangerines? Please notice that you can only cut the stick between two adjacent fruits, that you cannot cut a fruit in half as this fruit would be no good to eat.

Input
The input consists of multiply test cases. The first line of each test case contains an integer, n(1 <= n <= 100000), indicating the number of the fruits on the stick. The next line consists of a string with length n, which contains only ‘H’ (means hawthorn) and ‘T’ (means tangerine).
The last test case is followed by a single line containing one zero.

Output
Output the minimum number of times that you need to cut the stick or “-1” if you cannot get an equal cut. If there is a solution, please output that cuts on the next line, separated by one space. If you cut the stick after the i-th (indexed from 1) fruit, then you should output number i to indicate this cut. If there are more than one solution, please take the minimum number of the leftist cut. If there is still a tie, then take the second, and so on.

Sample Input
4
HHTT
4
HTHT
4
HHHT
0

Sample Output
2
1 3
1
2
-1

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-29 04:53
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    # read data
    data <- readLines()
    
    # split into list
    list_data <- lapply(data, strsplit, split = "[^H|T]")
    
    # define function
    cut_ice_sugar_gourd <- function(s){
      # split again to separate each item
      s_list <- lapply(s, strsplit, split = " ")
      
      # check if there is any even number of items 
      if(sum(nchar(s_list[1])) %% 2 != 0){
        return(-1)
      }
      
      # check if there are enough items to make even cuts
      if(length(s_list) %/% 2 != 1){
        return(-1)
      }
      
      # find the number of even items
      even_num <- sum(s_list[1]) / 2
      
      # count how many cuts we need
      cut_count <- ceiling(log2(even_num))
      
      # if we have odd number of items, then we need an extra cut
      if(length(s_list) %/% 2 != 1){
        cut_count <- cut_count + 1
      }
      
      # calculate the cuts
      cuts <- paste(cut_count, collapse = " ")
    
      # return the answer
      return(list(s_list, cuts))
    }
    
    # run through all the tests
    for(i in 1:length(list_data)){
      result <- cut_ice_sugar_gourd(list_data[[i]])
      print(result$s_list, "\n", result$cuts)
    }
    

    这段代码使用了 R 脚本语言。

    评论

报告相同问题?