I was solving this problem on CodinGame.com and I managed to write a code that passed the system test for the last test case (a very large test case). But compiling on my laptop I get an output of 0 instead of 57330892800 which the code gave me from their machine. I compiled with both Visual Studio 2012 Express and Dev C++ 4.9.9.2.
I used a recursive function so if I had ran out of stack memory, I'd expected a stack overflow error but there was no error, nothing, just an output of 0. Why is this happening on my system while it worked just fine on the site's machine? What could have caused this, cos I doubt it is stack overflow?
#include<iostream>
#include<algorithm>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<vector>
using namespace std;
typedef long long LONG;
string X[]={".-","-...","-.-.","-..",
".","..-.","--.","....",
"..",".---","-.-",".-..",
"--","-.","---",".--.",
"--.-",".-.","...","-",
"..-","...-",".--","-..-",
"-.--","--.."};
map<string, int> dict;
string morse(const string ret){
string s;
for(char c : ret) s+=X[c-'A'];
return s;
}
LONG decode(int start, string &word, vector<LONG> &mem){
if(start == word.size()) return 1;
else if(mem[start] != -1) return mem[start];
LONG res = 0;
string s;
for(int i=0; i<19*4 && start+i<word.size(); i++){
s+=word[start+i];
auto curr = dict.find(s);
if(curr!=dict.end()) res+=curr->second*decode(start+i+1, word, mem);
}
mem[start]=res;
return res;
}
int main(){
string word;
cin>>word;
int n; cin>>n;
for(int i=0; i<n; i++){
string s;
cin>>s;
dict[morse(s)]++;
}
vector<LONG> mem(word.size(), -1);
cout<<decode(0, word, mem)<<endl;
}