Блог пользователя rachitnaruzu

Автор rachitnaruzu, 9 лет назад, По-английски

link to problem: Watto and Mechanism

I used C++ maps and mapped all n strings in map, initially, like this:

//map<string,bool> m;
m[str] = true;

then for each query string s of length L, produced 3*L modified_strings (meaning: at every position in original string, putting 'a','b','c' to produce 3*L modified _strings with only one character difference from s) and check whether any of them are in map, like this:

//let t be one of the modified string
if(m[modified_string]) flag = true;

if any one of them is present in map then print "YES" otherwise "NO". But this solution is giving Wrong answer on pretest 6. Here 10118407 is my code. And secondly, i was not able to get the idea of using polynomial hashing, can anyone please explain ?

  • Проголосовать: нравится
  • -3
  • Проголосовать: не нравится

»
9 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Map isn't magic, you can't insert so many strings, because of memory limits...

  • »
    »
    9 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Thank you for your reply but could you please explain what has to be done, i was not able to get the solution to this one ? How to accomplish the task of saving strings, because they have to be saved somehow, i guess.

    • »
      »
      »
      9 лет назад, # ^ |
      Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

      http://codeforces.com/contest/514/problem/C

      Given string s, determine if the memory of the mechanism contains string t that consists of the same number of characters as s and differs from s in exactly one position.

      Besides of the fact this solution probably TLEs on large testcases (you consider something like 3m checks in your map, each of them may need to compare very long strings), you make some checks for strings t which are equal to s.

      E.g.

      1 2
      aaa
      aab
      aaa
      

      Correct:

      YES
      NO
      

      Your output (I guess):

      YES
      YES
      
      • »
        »
        »
        »
        9 лет назад, # ^ |
        Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

        Thanks a lot. You were right, silly of me. After handling the above test case, it passed me for pretest 6 (just checked that if modified_string is in map then it must not be same as s).

        10122988 is my new code.

        But as kpw29 said, i'm getting "Memory limit exceeded" on test 18 now.

        Now if i rethink, my approach is totally wrong i guess, could you please tell me what is the efficient approach to this one ?

        • »
          »
          »
          »
          »
          9 лет назад, # ^ |
          Rev. 2   Проголосовать: нравится +1 Проголосовать: не нравится

          You should use polynomial hash (See the editorial) or a trie for a good solution

          On another note, you should not check the existence of a key in a map by mp[key] like in your code. When you write mp[key] it creates a new entry in the map with that key and returns the new value which is initialized to the default value of the type(bool in your case). AFAIK default value of a primitive type is undefined, but seeing that your code has passed 17 pretests, it must be initializing them to false.

          Your solution might pass a couple more cases (atleast avoid MLE, though it will TLE) if you check existence in the map using map.find

          My solution using a trei — 10057709

          My solution using 2x Hash — 9866391