-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalienmath.cpp
More file actions
63 lines (50 loc) · 1.51 KB
/
alienmath.cpp
File metadata and controls
63 lines (50 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Fast IO
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// Get the number of digits i.e. the base
int b;
cin >> b;
// Maps each digit to it's decimal equivalent
map<string, int> digitToIndex;
// For each digit
for(int i = 0 ; i < b; i++)
{
// Get the digit
string x;
cin >> x;
// Map this digit to the decimal equivalent
digitToIndex[x] = i;
}
long long total = 0; // Stores the output as a decmial
// Get the alien number
string num;
cin >> num;
int s = 0; // The starting index of there current digit
int e = 1; // Length of the current digit
// For each character in the alien number
for(int i = 1; i <= num.size(); i++)
{
// Get the current substring
string sub = num.substr(s,e);
// If the current substring isn't a digit
if(digitToIndex.count(sub) == 0)
{
e++; // Increase the substring size
continue; // Get the next character
}
// If the current substring is a digit
else
{
total *= b; // Move the current total to the next column over
total += digitToIndex[sub]; // Add this digit to the total
s = i; // Set the starting index of the current digit to the next character
e = 1; // Set the length of the current digit to 1
}
}
cout << total << endl;
return 0;
}