2023年8月1日 星期二

[649] Dota2 Senate

題意真心看不懂 囧
終於搞懂之後,覺得想要用stack 解
寫出來還蠻快的!也沒什麼bug XD 大心!!!

char * predictPartyVictory(char * senate){
int popCount=0;
int stackIdx=-1;
int len = strlen (senate);
char stack[len];
char secondRound[len];

while(len>0)
{
for (int i=0;i<len;i++)
{
if (stackIdx <0 || (senate[i]==stack[stackIdx]))
{
stack[++stackIdx] = senate[i];
continue;
}
//pop
else //(senate[i]!=stack[stackIdx])
{
secondRound[popCount++]=stack[stackIdx--];
}
}
if (popCount==0) // no pop means all the same char
break;

memcpy(senate, secondRound, popCount);
len = popCount;
popCount=0;
}
if(stack[stackIdx]=='D')
return "Dire";
else
return "Radiant";
}

沒有留言:

張貼留言