【问题标题】:How to Determine the K^th character in the concatenated string如何确定连接字符串中的第 K^th 个字符
【发布时间】:2020-09-16 19:22:03
【问题描述】:

给定一个包含 N 个小写英文字母字符串的列表。可以找到任意数量的连续字符串一起形成一个新字符串。分组函数接受两个整数 XY 并连接索引 XY(包括)和返回一个修改后的字符串,其中连接字符串的字母被排序。

您会被问到 Q 个问题,每个问题都包含两个整数 LR。确定$K^{th}$。如果我们将 LR 传递给分组函数,则连接字符串中的字符。

输入格式

  • 第一行:N(列表中的字符串数)
  • 接下来的 N 行:字符串 $S_i$
  • 下一行Q(问题数)
  • 接下来的 Q 行:三个以空格分隔的整数 LRK

输出格式

  • 对于每个问题,在新行中打印连接字符串的 $K^{th}$ 字符。

示例测试用例

Sample Input                 Sample Output

5                                 c
aaaaa                             d
bbbbb                             e
ccccc
ddddd
eeeee
3
3 3 3 
1 5 16
3 5 15

说明

  • Q1 分组字符串 - ccccc。第三个字符是 c
  • Q2 分组字符串 - aaaaabbbbbcccccdddddeeeee。第 16 个字符是 d
  • Q3 分组字符串 - cccccdddddeeeee。第 15 个字符是 e

注意:始终保证$K^{th}$位置有效

上下文:

这个问题是在我 10 月份参加的招聘挑战中提出的。我只是想起了这个问题,并认为我可以试一试,但我仍在努力解决这个问题。

在线没有有效的解决方案,因此我将访问该网站作为最后的手段。任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: python r string


    【解决方案1】:

    我尝试使用 Python 3 解决该问题,这个问题是在 HackerEarth 评估中提出的。

    # Input code:
    n = int(input())
    arr = []
    for _ in range(n):
        arr.append(input().strip())
    p = int(input())
    m = 3
    Q = []
    for _ in range(p):
        Q.append(list(map(int, input().split())))
    
    out_ = solve(Q, arr)
    print('\n'.join(out_))
    

    解决方案:

    def solve(Q, arr):
        ans= [] # empty list to append the kth element for each Q
        for i in range(len(Q)): 
            L, R, K = Q[i] # We assign value to L, R, K
            # We join string from arr between index "L-1" and "R" (for zero indexing) and get element at Kth index (K-1)
            ans.append("".join(arr[L-1:R])[K-1])
        return ans
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案2】:

    我已经使用 python3 来演示答案。尝试尽可能多地发表评论以使其不言自明。

    回复:https://repl.it/@PavitraBehre/Kth-Character-in-Concatenated-String

    #No of strings
    n = int(input())
    
    #Empty list to contain input strings
    strings = []
    
    #Looping N times, using _ as control variable as it's not needed anywhere
    for _ in range(n):
      #Appending the stipped input string to lit
      strings.append(input().strip())
    
    #Getting no of questions
    q = int(input())
    
    #Looping Q times
    for _ in range(q):
      #Getting L,R,K as mapped tuples from input as int
      L,R,K = map(int, input().split())
    
      #Printing the L from Strings List
      #Note: lists use 0 based indexing and question has 1 based indexing
      print("L:", strings[L-1])
    
      #Printing the R from Strings List
      print("R:", strings[R-1])
    
      #Using List Splicing and join function to join the string
      #list[start:stop:step]
      #Splicing doesn't include the last index of stop value thus no R-1
      s = "".join(strings[L-1:R])
      print("L<->R: ", s)
    
      #Printing Kth Character (1 based indexing)
      print("K:", s[K-1])
    

    【讨论】:

      【解决方案3】:

      假设输入字符串存储在名为单词的字符串数组中。构建一个 N * 26 矩阵来存储每个字符串的字符数。 mat[i][0] 将在 words[i] 中给出 a 的计数,依此类推。这是只需要进行一次的预处理

          
          for(int i=0;i<words.size();i++){
              for(char ch : words[i]){
                  mat[i][ch-'a']++;
              }
          }
      

      现在对于每个查询,调用函数 determine_character()。对 (l,r) 范围内的每个字符进行迭代背后的逻辑是,在连接之后,我们将得到的字符串是排序的。

      char determine_character(vector<string> words, int l, int r, int k){    
          int sum=0;
          for(int i=0;i<26;i++){
               for(int j= l-1;j<=r-1;j++)
                  sum += mat[j][i];
              
               if(sum>=k)
                  return 'a' + i;    
          }
          return 'z';
      }
      

      【讨论】:

      • 比其他解决方案更好。我认为仍然不完美。假设字符串总数为 10^5,查询总数为 10^5,并且在每个查询中 i = 0 和 j = 10^5,那么您的解决方案的复杂性变为:10^ 5 * 26 * 10^ 5我仍在研究解决方案,但似乎必须在预处理矩阵后使用段树。
      【解决方案4】:

      一旦你理解了问题,代码本身就很简单了。在这里,我将演示如何使用 R。

      让我们从定义 S 开始,我们得到的字符串集合:

      S <- letters[1:5]
      S <- paste0(S, S, S, S, S)
      S
      #> [1] "aaaaa" "bbbbb" "ccccc" "ddddd" "eeeee"
      

      现在我们可以很容易地将 Lth 连接到字符串向量的 Rth 字符串,Str 这样做很容易

      concat <- function(Str, L, R) paste0(Str[L:R], collapse = "")
      

      因此,例如,如果给定 L = 1 和 R = 2,我们可以这样做:

      concat(S, 1, 2)
      [1] "aaaaabbbbb"
      

      此外,我们可以像这样在任意字符串StrK位置找到字符:

      char_at <- function(Str, K) substr(Str, K, K)
      

      所以如果我们将这两者结合起来,我们会得到:

      f <- function(L, R, K) char_at(concat(S, L, R), K)
      

      例如:

      # Q1
      f(3, 3, 3)
      #> [1] "c"
      
      # Q2
      f(1, 5, 16)
      #> [1] "d"
      
      # Q3
      f(3, 5, 15)
      #> [1] "e"
      

      【讨论】:

      • 他真的不是在寻求蛮力解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-25
      • 2019-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多