How can I make my game more effective?

I am working on the problem of ranking hackers, and I believe my decision is correct. However, most of my test cases are wait times. Can anyone suggest how to make my code more efficient?

The important part of the code starts with the "Trie Class".

The exact question can be found here: https://www.hackerrank.com/challenges/contacts

using System; using System.Collections.Generic; using System.IO; class Solution { static void Main(String[] args) { int N = Int32.Parse(Console.ReadLine()); string[,] argList = new string[N, 2]; for (int i = 0; i < N; i++) { string[] s = Console.ReadLine().Split(); argList[i, 0] = s[0]; argList[i, 1] = s[1]; } Trie trie = new Trie(); for (int i = 0; i < N; i++) { switch (argList[i, 0]) { case "add": trie.add(argList[i, 1]); break; case "find": Console.WriteLine(trie.find(argList[i, 1])); break; default: break; } } } } class Trie { Trie[] trieArray = new Trie[26]; private int findCount = 0; private bool data = false; private char name; public void add(string s) { s = s.ToLower(); add(s, this); } private void add(string s, Trie t) { char first = Char.Parse(s.Substring(0, 1)); int index = first - 'a'; if(t.trieArray[index] == null) { t.trieArray[index] = new Trie(); t.trieArray[index].name = first; } if (s.Length > 1) { add(s.Substring(1), t.trieArray[index]); } else { t.trieArray[index].data = true; } } public int find(string s) { int ans; s = s.ToLower(); find(s, this); ans = findCount; findCount = 0; return ans; } private void find(string s, Trie t) { if (t == null) { return; } if (s.Length > 0) { char first = Char.Parse(s.Substring(0, 1)); int index = first - 'a'; find(s.Substring(1), t.trieArray[index]); } else { for(int i = 0; i < 26; i++) { if (t.trieArray[i] != null) { find("", t.trieArray[i]); } } if (t.data == true) { findCount++; } } } } 

EDIT: I made some suggestions in the comments, but realized that I cannot replace s.Substring (1) with s [0] ... because I really need s [1 .. n]. AND s [0] returns char, so I will need to do that. ToString on it anyway.

Also, to add a little more information. The idea is that he needs to count ALL names after the prefix, for example.

 Input: "He" Trie Contains: "Hello" "Help" "Heart" "Ha" "No" Output: 3 
+5
source share
2 answers

I could just post a solution here that will give you 40 points, but I think it will not be fun.

  • Use Enumerator <char> instead of any string operations
  • Count when adding values
  • any charachter minus 'a' makes a nice index array (gives values ​​from 0 to 25)

enter image description here

+2
source

I think this link should be very useful.

There you can find a good implementation of the trie data structure, but it is a Java implementation. I implemented trie in C # with this wonderful article a year ago, and I tried to find a solution to another hackerran task). It was a success.

enter image description here

In my problem, I had just three (I mean that my alphabet is 2) and only one method to add new nodes:

 public class Trie { //for integer representation in binary system 2^32 public static readonly int MaxLengthOfBits = 32; //alphabet trie size public static readonly int N = 2; class Node { public Node[] next = new Node[Trie.N]; } private Node _root; } public void AddValue(bool[] binaryNumber) { _root = AddValue(_root, binaryNumber, 0); } private Node AddValue(Node node, bool[] val, int d) { if (node == null) node = new Node(); //if least sagnificient bit has been added //need return if (d == val.Length) { return node; } // get 0 or 1 index of next array(length 2) int index = Convert.ToInt32(val[d]); node.next[index] = AddValue(node.next[index], val, ++d); return node; } 
+1
source

Source: https://habr.com/ru/post/1241900/


All Articles