baddestguy's blog

By baddestguy, history, 4 years ago, In English

I want to find the number of strings that are lexicographically smaller than A and has substring B NOTE: the length of the strings should be <= |A|.

e.g. A = "da" and B = "c" then answer = 29: {'c', 'ac', 'bc', cX}, where X is any alphabet. also, constraints are pretty low, 1 <= |B|,|A| <= 1000 Please help thanks.

  • Vote: I like it
  • +2
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Does "contains" mean "subsequence" or "substring"? Is "smaller" lexicographically smaller or anything else?

  • »
    »
    4 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    it means substring, and smaller means lexicographically smaller

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Is the number of such strings not infinite in your case? "c", "ca", "caa", "caaa", "caaaa" etc all have "c" as a substring and are lexicographically smaller than "da".

      • »
        »
        »
        »
        4 years ago, # ^ |
        Rev. 3   Vote: I like it 0 Vote: I do not like it

        Oh, then the length of the strings should be <= |A|.

        is there a name for this type of ordering cause I think I'm not phrasing it correctly?

        • »
          »
          »
          »
          »
          4 years ago, # ^ |
            Vote: I like it +1 Vote: I do not like it

          I don't think it has a standard name, you just had to be more explicit.

          Anyway, I think your problem can be solved with dynamic programming, states like this: $$$\mathrm{dp}[i][j][a][b]$$$ counts the number of strings lexicographically smaller than $$$A$$$, where:

          • $$$i$$$ is the length of the string;
          • $$$j$$$ is the length of the longest suffix of the string that is also a prefix of $$$B$$$;
          • $$$a$$$ is 0 if the string is a prefix of $$$A$$$, 1 otherwise;
          • $$$b$$$ is 0 if the string does not contain $$$B$$$ as a substring, 1 otherwise.

          I think you should be able to do updates in a KMP-like way.