Can someone help me with the stdin and stdout for node.js. My code is as follows
process.stdin.resume();
process.stdin.setEncoding(‘utf8’);
process.stdin.on(‘data’, function (chunk) {
var lines = chunk.toString().split(’\n’);
var stringDiff = function(a,b){
var aSet = getAllSubstring(a);
var bSet = getAllSubstring(b);
var nSet = difference(aSet, bSet);
nSet = nSet.concat(difference(bSet, aSet));
return nSet.length;
}
var difference = function(a,b){
var nSet = [];
for(var i in a){
var found = false;
for(x in b){
if(a[i] === b[x]){
found = true;
break;
}
}
if(!found){ nSet.push(a[i])};
}
return nSet;
}
var getAllSubstring = function(s){
var aSet = [];
var i = 1;
var j = 0;
while(i<=s.length&&j<s.length){
var next = s.substring(j,i);
if(aSet.indexOf(next)<0){
aSet.push(next);
}
if(i===s.length){
j++;
i = j+1;
}else{
i++;
}
}
return aSet;
}
var diff = stringDiff(lines[0], lines[1]);
process.stdout.write(diff+'\n');
process.exit();
getting run time error NZEC. first submission in codechef. so dont know much abt online compilers. plz help me out sir CodeChef: Practical coding for everyone
Yes @acmonster construct LCP array using hashes in O(N log^2 N) time. We can compare two suffixes in O(log N) time using hashes. So we can sort them in O(N log^2 N) time. And then calculate LCP array in O(N log N) time.
In the stanford link ‘)’ is part of the hyperlink pasted here . Remove that and paste the link in your browser and you will able to access the pdf .
Or use this link : http://www.stanford.edu/class/cs97si/suffix-array.pdf
Try large test where both strings have equal characters.
The first test in the system has the form
string(99990,‘x’)
string(99969,‘x’)
and the answer obviously is 21,
while your answer is 25769803797.