thank you so much for your answer Frederic
I changed the code to the following, now, it passes first test set and fails
second(with WA)
############ new code ############
const readline = require('readline');
var rl = readline.createInterface(process.stdin, process.stdout);
var TCNum=0;
var TCmax=null;
var waitFor='T';
var nl;
rl.on('line', function(data) {
if(waitFor=='T'){
TCmax=parseInt(data);
waitFor='NL';
}else if(waitFor=='NL'){
nl=data.split(' ');
waitFor='STR';
}else if(waitFor=='STR'){
var list=data.split(' ');
var key=findKey(list);
var primerifiedInfo=primify(key.key,key.index,list);
var dictionary=creatDictionary(primerifiedInfo.primes);
var decrypted=decrypt(dictionary,primerifiedInfo.list).join('');
console.log("Case #"+(TCNum+1)+": "+decrypted);
waitFor='NL';
checkCount();
}
function checkCount()
{
TCNum++;
if(TCNum==TCmax)
{
rl.close();
}
}
}).on('close',function(){
process.exit(0);
});
function gcd_recursive(a,b)
{
if (b)
{
return gcd_recursive(b, a % b);
}else{
return a;
}
}
function findKey(list)
{
for(var i=0;i<list.length;i++)
{
if(list[i]!=list[i+1])
{
return {key:list[i]/gcd_recursive(list[i],list[i+1]),index:i};
}
}
}
function primify(key,index,list)
{
var allPrimes=[];
var subKey=key;
var out=[key];
var thisKey;
for(let i=index;i<list.length;i++)
{
allPrimes=addMemberToSet(subKey,allPrimes);
thisKey=list[i]/subKey;
out.push(thisKey);
subKey=thisKey;
}
allPrimes=addMemberToSet(subKey,allPrimes);
if(index>0)
{
subKey=key;
for(let i=index-1;i>=0;i--)
{
allPrimes=addMemberToSet(subKey,allPrimes);
thisKey=list[i]/subKey;
out.unshift(thisKey);
subKey=thisKey;
}
}
out={list:out,primes:allPrimes};
return out;
}
function creatDictionary(primes)
{
var tmp;
for(let i=primes.length-1;i>=0;i--)
{
for(let ii=0;ii<i;ii++)
{
if(primes[ii+1]<primes[ii])
{
tmp=primes[ii];
primes[ii]=primes[ii+1];
primes[ii+1]=tmp;
}
}
}
var dictionary={};
for(let i=0;i<primes.length;i++)
{
dictionary[primes[i]]=String.fromCharCode(i+65);
}
return dictionary;
}
function decrypt(dictionary,primerifiedList)
{
var out=[];
for(let i=0;i<primerifiedList.length;i++)
{
out[i]=dictionary[primerifiedList[i]];
}
return out;
}
function addMemberToSet(member,toSet)
{
var repeated=false;
toSet.forEach(function(token){
if(token===member)
{
repeated=true;
}
});
if(repeated)
{
return toSet;
}
toSet.push(member);
return toSet;
}
###########################
--
You received this message because you are subscribed to the Google Groups
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-code/f3838beb-0482-47ee-93cb-4b652d95851c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.