
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to check whether first player can win a game where players can form string char by char in C++
Suppose we have a list of words. Now consider a ghost game where two players can participate into it. Here players alternate appending letters to a string. And the string that is being made must be a valid prefix of a word in the list, and the player who spells out any word in the list loses. We have to check whether the first player can win or not if both players are playing optimally.
So, if the input is like words = ["manage", "manager", "min"], then the output will be True, as they can play like −
- m [Player 1]
- ma [Player 2]
- man [Player 1]
- mana [Player 2]
- manag [Player 1]
- manage [Player 2] Lose
So player 1 wins.
To solve this, we will follow these steps −
- Define one map mp
- for each word it in words, do
- ch := it[0]
- insert it into mp[ch]
- mn := inf
- for each key-value pair it in mp, do
- str := value of it
- size := size of str
- if size mod 2 is same as 0, then −
- return 1
- return 0
Example (C++)
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; bool solve(vector<string> &words) { map<char, set<string>> mp; for (auto &it : words) { char ch = it[0]; mp[ch].insert(it); } int mn = INT_MAX; for (auto &it : mp) { string str = *(it.second.begin()); int size = str.size(); if (size % 2 == 0) return 1; } return 0; } int main(){ vector<string> v = {"manage", "manager", "min"}; cout << solve(v); }
Input
{"manage", "manager", "min"}
Output
1
- Related Articles
- Program to check whether first player win in candy remove game or not in Python?
- Program to check first player can win by reaching total sum to target in Python
- Program to check whether Amal can win stone game or not in Python
- How can we check that by default MySQL CHAR() function returns a binary string?
- Minimum Players required to win the game in C++
- Program to check whether first player can take more candies than other or not in Python
- Program to check person 1 can win the candy game by taking maximum score or not in Python
- Program to check whether we can form 24 by placing operators in python
- How to convert a std::string to const char* or char* in C++?
- Java Program to convert Char array to String
- How to convert an std::string to const char* or char* in C++?
- How can we produce a string, other than default binary string, in a given character set by MySQL CHAR() function?
- C++ program to count how many ways two players win or make draw in dice throwing game
- Copy char array to string in Java
- Convert string to char array in Java

Advertisements