📌  相关文章
📜  通过擦除任何两个连续的相似字母来找到获胜游戏的获胜者

📅  最后修改于: 2021-09-24 04:57:24             🧑  作者: Mango

给定一个由小写字母组成的字符串。
游戏规则:

  • 玩家可以选择一对相似的连续字符并删除它们。
  • 有两个玩家在玩游戏,最后一步的玩家获胜。

任务是找到获胜者,如果 A 先走并且双方都发挥最佳。
例子:

Input: str = "kaak" 
Output: B
Explanation:
    Initial String: "kaak"
    A's turn:
        removes: "aa"
        Remaining String: "kk"
    B's turn:
        removes: "kk"
        Remaining String: ""
    Since B was the last one to play
    B is the winner.

Input: str = "kk"
Output: A

方法:我们可以使用堆栈来简化问题。

  • 每次我们遇到一个与堆栈顶部的字符不同的字符,我们将其添加到堆栈中。
  • 如果栈顶和下一个字符匹配,我们从栈中弹出字符并增加计数。
  • 最后,我们只需要通过检查 count%2 来看看谁赢了。

下面是上述方法的实现:

C++
#include 
using namespace std;
 
// Function to play the game
// and find the winner
void findWinner(string s)
{
    int i, count = 0, n;
    n = s.length();
    stack st;
 
    // ckecking the top of the stack with
    // the i th character of the string
    // add it to the stack if they are different
    // otherwise increment count
    for (i = 0; i < n; i++) {
        if (st.empty() || st.top() != s[i]) {
            st.push(s[i]);
        }
        else {
            count++;
            st.pop();
        }
    }
 
    // Check who has won
    if (count % 2 == 0) {
        cout << "B" << endl;
    }
    else {
        cout << "A" << endl;
    }
}
 
// Driver code
int main()
{
    string s = "kaak";
 
    findWinner(s);
 
    return 0;
}


Java
// Java implementation for above approach
import java.util.*;
 
class GFG
{
 
// Function to play the game
// and find the winner
static void findWinner(String s)
{
    int i, count = 0, n;
    n = s.length();
    Stack st = new Stack();
 
    // ckecking the top of the stack with
    // the i th character of the string
    // add it to the stack if they are different
    // otherwise increment count
    for (i = 0; i < n; i++)
    {
        if (st.isEmpty() ||
            st.peek() != s.charAt(i))
        {
            st.push(s.charAt(i));
        }
        else
        {
            count++;
            st.pop();
        }
    }
 
    // Check who has won
    if (count % 2 == 0)
    {
        System.out.println("B");
    }
    else
    {
        System.out.println("A");
    }
}
 
// Driver code
public static void main(String[] args)
{
    String s = "kaak";
 
    findWinner(s);
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the approach
 
# Function to play the game
# and find the winner
def findWinner(s) :
 
    count = 0
    n = len(s);
    st = [];
 
    # ckecking the top of the stack with
    # the i th character of the string
    # add it to the stack if they are different
    # otherwise increment count
    for i in range(n) :
        if (len(st) == 0 or st[-1] != s[i]) :
            st.append(s[i]);
             
        else :
            count += 1;
            st.pop();
 
    # Check who has won
    if (count % 2 == 0) :
        print("B");
     
    else :
        print("A");
         
# Driver code
if __name__ == "__main__" :
 
    s = "kaak";
 
    findWinner(s);
 
# This code is contributed by AnkitRai01


C#
// C# implementation for above approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
// Function to play the game
// and find the winner
static void findWinner(String s)
{
    int i, count = 0, n;
    n = s.Length;
    Stack st = new Stack();
 
    // ckecking the top of the stack with
    // the i th character of the string
    // add it to the stack if they are different
    // otherwise increment count
    for (i = 0; i < n; i++)
    {
        if (st.Count == 0 ||
            st.Peek() != s[i])
        {
            st.Push(s[i]);
        }
        else
        {
            count++;
            st.Pop();
        }
    }
 
    // Check who has won
    if (count % 2 == 0)
    {
        Console.WriteLine("B");
    }
    else
    {
        Console.WriteLine("A");
    }
}
 
// Driver code
public static void Main(String[] args)
{
    String s = "kaak";
 
    findWinner(s);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
B

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程