Ref Link
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
map<char,int> memo;
int main()
{
int t, next;
string s;
cin >> t;
for(int k = 1; k <= t; k++)
{
cin >> s;
memo.clear();
// number does not start with 0 so min value for the first char is 1
memo[s[0]] = 1;
//next value to assign to a symbol
next = 0;
for(int i = 1; i < s.size(); i++)
{
if(memo.count(s[i]) == 0)
{
memo[s[i]] = next++;
// 1 is already used so increment next value once again
if(next == 1)
{
next++;
}
}
}
long long value = 0;
// base will be number of unique character in the string or the highest value assigned to any character
int base = (next == 0) ? 2 : next;
for(int i = 0; i < s.size(); i++)
{
value = value * base + memo[s[i]];
}
cout << "Case #" << k << ": " << value << endl;
}
return 0;
}
Comments
Post a Comment