zoukankan      html  css  js  c++  java
  • Hackerrank--Ashton and String(后缀数组)

    题目链接

    Ashton appeared for a job interview and is asked the following question. Arrange all the distinct substrings of a given string in lexicographical order and concatenate them. Print the Kth character of the concatenated string. It is assured that given value of K will be valid i.e. there will be a Kth character. Can you help Ashton out with this?

    Input Format 
    First line will contain a number T i.e. number of test cases. 
    First line of each test case will contain a string containing characters (az) and second line will contain a number K.

    Output Format 
    Print Kth character ( the string is 1 indexed )

    Constraints 
    1T5 
    1length105 
    K will be an appropriate integer.

    Sample Input #00

    1
    dbac
    3
    

    Sample Output #00

    c
    

    Explanation #00

    The substrings when arranged in lexicographic order are as follows

    a, ac, b, ba, bac, c, d, db, dba, dbac
    

    On concatenating them, we get

    aacbbabaccddbdbadbac
    

    The third character in this string is c and hence the answer.

     

    题意:给出一个字符串,把这个字符串的所有字串按字典序升序拼接起来得到一个大的字符串。求该字符串的第k个字符。

    思路:该字符串的字串按字典序排序后的结果其实就是后缀数组中根据lcp一个个数出来的字串。。。

    Accepted Code:

     1 #include <string>
     2 #include <iostream>
     3 #include <algorithm>
     4 using namespace std;
     5 
     6 const int MAX_N = 100005;
     7 typedef long long LL;
     8 int n, k, sa[MAX_N], rk[MAX_N], lcp[MAX_N], tmp[MAX_N];
     9 
    10 bool compare_sa(int i, int j) {
    11     if (rk[i] != rk[j]) return rk[i] < rk[j];
    12     int ri = i + k <= n ? rk[i + k] : -1;
    13     int rj = j + k <= n ? rk[j + k] : -1;
    14     return ri < rj;
    15 }
    16 
    17 void construct_sa(const string &S, int *sa) {
    18     n = S.length();
    19     for (int i = 0; i <= n; i++) {
    20         sa[i] = i;
    21         rk[i] = (i < n ? S[i] : -1);
    22     }
    23     
    24     for (k = 1; k <= n; k *= 2) {
    25         sort(sa, sa + n + 1, compare_sa);
    26         
    27         tmp[sa[0]] = 0;
    28         for (int i = 1; i <= n; i++) {
    29             tmp[sa[i]] = tmp[sa[i - 1]] + (compare_sa(sa[i - 1], sa[i]) ? 1 : 0);
    30         }
    31         for (int i = 0; i <= n; i++) rk[i] = tmp[i];
    32     }
    33 }
    34 
    35 void construct_lcp(const string &S, int *sa, int *lcp) {
    36     n = S.length();
    37     for (int i = 0; i <= n; i++) rk[sa[i]] = i;
    38     
    39     int h = 0;
    40     lcp[0] = 0;
    41     for (int i = 0; i < n; i++) {
    42         int j = sa[rk[i] - 1];
    43         
    44         if (h > 0) h--;
    45         for (; i + h < n && j + h < n; h++) if (S[i + h] != S[j + h]) break;
    46         
    47         lcp[rk[i] - 1] = h;
    48     }
    49 } 
    50 
    51 string S;
    52 
    53 void solve(LL k) {
    54     n = S.length();
    55     construct_sa(S, sa);
    56     construct_lcp(S, sa, lcp);   
    57     
    58     for (int i = 0; i < n; i++) {
    59         int L = lcp[i];
    60         int left = n - sa[i + 1];
    61         LL sum = (L + 1 + left) * (LL)(left - L) / 2;
    62         if (k > sum) {k -= sum;}
    63         else {
    64             for (int j = L + 1; j <= left; j++) {
    65                 if (k <= j) {
    66                    int index = sa[i + 1] + k;
    67                    cout << S[index - 1] << endl;
    68                    return ;
    69                 } else {
    70                     k -= j;
    71                 }
    72             }
    73         }
    74     }
    75 }
    76 int main(void) {
    77     ios::sync_with_stdio(false);
    78     int T;
    79     cin >> T;
    80     while (T--) {
    81         LL k;
    82         cin >> S >> k;
    83         solve(k);
    84     }
    85     return 0;
    86 }
  • 相关阅读:
    (二)常问升级小点
    (一)常问基础小点
    Linux之expr命令详解
    Mac--Visual Studio Code 常用快捷键
    git撤销commit操作回到add状态
    ExampleMatcher ,在查询非int 或boolean 字段时要使用 withIgnorePaths() 忽略 int 和boolean 字段,要不然查询不到值
    navicat 用url 连接mongo
    javase 打印杨辉三角
    关系型数据库遵循ACID规则
    Redis介绍
  • 原文地址:https://www.cnblogs.com/Stomach-ache/p/3929472.html
Copyright © 2011-2022 走看看