Contacts
We're going to make our own Contacts application! The application must perform two types of operations:
add name
, where is a string denoting a contact name. This must store as a new contact in the application.find partial
, where is a string denoting a partial name to search the application for. It must count the number of contacts starting with and print the count on a new line.
Given sequential add and find operations, perform each operation in order.
Input Format
The first line contains a single integer, , denoting the number of operations to perform.
Each line of the subsequent lines contains an operation in one of the two forms defined above.
Each line of the subsequent lines contains an operation in one of the two forms defined above.
Constraints
- It is guaranteed that and contain lowercase English letters only.
- The input doesn't have any duplicate for the operation.
Output Format
For each
find partial
operation, print the number of contact names starting with on a new line.
Sample Input
4
add hack
add hackerrank
find hac
find hak
Sample Output
2
0
Explanation
We perform the following sequence of operations:
- Add a contact named
hack
. - Add a contact named
hackerrank
. - Find and print the number of contact names beginning with
hac
. There are currently two contact names in the application and both of them start withhac
, so we print on a new line. - Find and print the number of contact names beginning with
hak
. There are currently two contact names in the application but neither of them start withhak
, so we print on a new line.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class trieNode {
public:
int val;
trieNode *next[26];
trieNode() {
val = 0;
}
};
class Trie {
private:
trieNode *root;
public:
Trie() {
for (int i = 0; i < 26; i++) {
root = new trieNode();
root -> next[i] = NULL;
}
}
void insert(string input) {
if (input.size() == 0) {
return;
}
trieNode *trav = root;
for (int i = 0; i < input.size();i++) {
int index = input[i] - 'a';
if (trav -> next[index] == NULL) {
trav -> next[index] = new trieNode();
}
trav -> next[index] -> val++;
trav = trav -> next[index];
}
}
int search(string input) {
if (input.size() == 0) {
return 0;
}
trieNode *trav = root;
for (int i = 0; i < input.size();i++) {
int index = input[i] - 'a';
if (trav -> next[index] == NULL) {
return 0;
}
trav = trav -> next[index];
}
return trav -> val;
}
~Trie() {
deleteTrie(root);
}
void static deleteTrie(trieNode *rot) {
if (rot == NULL) {
return;
}
for (int i = 0; i < 26; i++) {
if (rot -> next[i] != NULL) {
deleteTrie(rot -> next[i]);
}
}
delete rot;
}
};
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n;
cin >> n;
Trie* trie = new Trie();
string input, op;
for (int i = 0; i < n; i++) {
cin >> op >> input;
if (op == "add") {
trie->insert(input);
} else if (op == "find") {
cout << trie->search(input) << endl;
}
}
delete trie;
return 0;
}
========
Find the Running Median
The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then:
- If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set , is the median.
- If your set contains an even number of elements, the median is the average of the two middle elements of the sorted sample. In the sorted set , is the median.
Given an input stream of integers, you must perform the following task for each integer:
- Add the integer to a running list of integers.
- Find the median of the updated list (i.e., for the first element through the element).
- Print the list's updated median on a new line. The printed value must be a double-precision number scaled to decimal place (i.e., format).
Input Format
The first line contains a single integer, , denoting the number of integers in the data stream.
Each line of the subsequent lines contains an integer, , to be added to your list.
Each line of the subsequent lines contains an integer, , to be added to your list.
Constraints
Output Format
After each new integer is added to the list, print the list's updated median on a new line as a single double-precision number scaled to decimal place (i.e., format).
Sample Input
6
12
4
5
3
8
7
Sample Output
12.0
8.0
5.0
4.5
5.0
6.0
Explanation
There are integers, so we must print the new median on a new line as each integer is added to the list:
No comments:
Post a Comment