Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 91 additions & 93 deletions Casino_Game.cpp
Original file line number Diff line number Diff line change
@@ -1,100 +1,98 @@
#include<iostream>
#include<string>
#include<cstdlib>//header file which have random number gen function
#include<ctime>

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;
void drawline(int n,char symbol);
void rules();

int main()
{
string playername;
int amount;
int bettingamount;
int guess;//this will store the number given by user
int dice;//this will store the number given by computer
char choice;

srand(time(0));//this will seed the number(random number gen function)

drawline(60,'_');
cout<<"\n\n\n\t\tCASINO GAME\n\n\n\n";
drawline(60,'_');

cout<<"\n\nEnter your name:";
getline(cin,playername);

cout<<"\n\nEnter money to play the game: $";
cin>>amount;

do
{
system("cls");
rules();
cout<<"\n\nyour current balance is $"<<amount<<"\n";
//getting players betting amount

do
{
cout<<playername<<" ,enter money to bet : $";
cin>>bettingamount;
if(bettingamount>amount)
cout<<"your betting amount is more than your current balance\n"
<<"\nRe-enter data\n";

}while(bettingamount > amount);
do
{
cout<<"guess a number between 1 to 10: ";
cin>>guess;
if(guess<=0 || guess>10)
cout<<"please check the number!! it should be between 1 to 10\n"<<"\nRe-enter data\n ";

}while(guess <= 0 || guess > 10);

dice = rand()%10 + 1; //this will genrate a random number between 1 to 10
if(dice==guess)
{
cout<<"\n\nGood Luck you won $"<<bettingamount*10;
amount=amount+bettingamount*10;
}
else
{
cout<<"Bad Luck this time you lost $"<<bettingamount;
amount=amount-bettingamount;
}
cout << "\nThe winning number was : " << dice <<"\n";
cout << "\n"<<playername<<", You have $ " << amount << "\n";
if(amount == 0)
{
cout << "You have no money to play ";

void drawLine(int n, char symbol);
void displayRules();

int main() {
string playerName;
int amount, bettingAmount, guess, dice;
char choice;

srand(static_cast<unsigned int>(time(0))); // Seed random number generator

drawLine(60, '_');
cout << "\n\n\n\t\tCASINO GAME\n\n\n\n";
drawLine(60, '_');

cout << "\n\nEnter your name: ";
getline(cin, playerName);

cout << "\n\nEnter money to play the game: $";
cin >> amount;

do {
system("cls"); // Clear screen
displayRules();
cout << "\n\nYour current balance is $" << amount << "\n";

// Getting player's betting amount
do {
cout << playerName << ", enter money to bet: $";
cin >> bettingAmount;
if (bettingAmount > amount) {
cout << "Your betting amount is more than your current balance\n"
<< "Re-enter data\n";
}
} while (bettingAmount > amount);

// Getting player's guess
do {
cout << "Guess a number between 1 to 10: ";
cin >> guess;
if (guess < 1 || guess > 10) {
cout << "Please check the number! It should be between 1 and 10\n"
<< "Re-enter data\n";
}
} while (guess < 1 || guess > 10);

// Generate random number between 1 and 10
dice = rand() % 10 + 1;
if (dice == guess) {
cout << "\n\nGood luck! You won $" << bettingAmount * 10 << "\n";
amount += bettingAmount * 10; // Update balance
} else {
cout << "Bad luck! You lost $" << bettingAmount << "\n";
amount -= bettingAmount; // Update balance
}

cout << "The winning number was: " << dice << "\n";
cout << playerName << ", you have $ " << amount << "\n";

if (amount <= 0) {
cout << "You have no money to play. Game over.\n";
break;
}
cout << "\n\n-->Do you want to play again (y/n)? ";

cout << "\n\nDo you want to play again (y/n)? ";
cin >> choice;
}while(choice =='Y'|| choice=='y');

cout<<"\n\n\n";
drawline(70,'=');
cout<<"\n\nthanks for playing this game, your cureent balance is $"<<amount<<"\n\n";

return 0;

}
void drawline(int n,char symbol)
{for(int i=0;i<n;i++)
cout<<symbol;
cout<<"\n";

} while (choice == 'Y' || choice == 'y');

cout << "\n\n";
drawLine(70, '=');
cout << "\n\nThanks for playing! Your current balance is $" << amount << "\n\n";

return 0;
}

void drawLine(int n, char symbol) {
cout << string(n, symbol) << "\n"; // Use string constructor for simplicity
}
void rules()
{
system("cls");
cout<<"\n\n";
drawline(80,'-');

void displayRules() {
system("cls"); // Clear screen
cout << "\n\n";
drawLine(80, '-');
cout << "\t\tRULES OF THE GAME\n";
drawline(80,'-');
cout << "\t1. Choose any number between 1 to 10\n";
cout << "\t2. If you win you will get 10 times of money you bet\n";
cout << "\t3. If you bet on wrong number you will lose your betting amount\n\n";
drawline(80,'-');
drawLine(80, '-');
cout << "\t1. Choose any number between 1 and 10\n";
cout << "\t2. If you win, you will get 10 times your bet amount\n";
cout << "\t3. If you bet on the wrong number, you will lose your betting amount\n\n";
drawLine(80, '-');
}
107 changes: 54 additions & 53 deletions manachers_algorithm.cpp
Original file line number Diff line number Diff line change
@@ -1,67 +1,68 @@
//Manacher Algorithm
/*To find the longest palindromic substring from a string, we can use Manacher’s Algorithm. By selecting each character, we will try to find if there any palindrome using left and right pointer. There is another array to store information, from that information we can easily find how long the palindrome is. For each character, the array will store information. After traversing the whole string, we can find the longest palindromic subsequence from the created array.
#include <iostream>
#include <string>
#include <vector>

The time complexity of this algorithm is O(n).*/
/*Enter String: levelup
Longest palindrome is: level*/

#include<iostream>
using namespace std;

int min(int a, int b) {
return (a<b)?a:b;
return (a < b) ? a : b;
}

string longestPalindrome(string mainString) {
int n = mainString.size();
if(n == 0)
return "";
n = 2*n + 1; //count the next position
int longPal[n]; //array to store longest palindrome length
longPal[0] = 0; longPal[1] = 1;
int centerIndex = 1;
int rightIndex = 2;
int right = 0, left;
int maxPalLength = 0, maxCenterIndex = 0;
int start = -1, end = -1, diff = -1;
string longestPalindrome(const string &mainString) {
if (mainString.empty()) return "";

// Transform the string to insert boundaries
string T = "^#"; // Starting with sentinel characters
for (char c : mainString) {
T += c;
T += '#';
}
T += '$'; // Ending with a sentinel

for (right = 2; right < n; right++) {
left = 2*centerIndex-right; //calculate left position using center and right
longPal[right] = 0;
diff = rightIndex - right;
int n = T.size();
vector<int> P(n, 0); // Array to store lengths of palindromes
int center = 0, right = 0;

for (int i = 1; i < n - 1; i++) {
int mirror = 2 * center - i; // Calculate the mirror index

if(diff > 0)
longPal[right] = min(longPal[left], diff);
while ( ((right + longPal[right]) < n && (right - longPal[right]) > 0) &&
( ((right + longPal[right] + 1) % 2 == 0) ||
(mainString[(right + longPal[right] + 1)/2] == mainString[(right - longPal[right] - 1)/2] ))) {
longPal[right]++;
}
if (right > i) {
P[i] = min(right - i, P[mirror]); // Use the mirror value if within bounds
}

if(longPal[right] > maxPalLength) { //max palindrome length
maxPalLength = longPal[right];
axCenterIndex = right;
}
// Expand around center i
while (T[i + P[i] + 1] == T[i - P[i] - 1]) {
P[i]++;
}

if (right + longPal[right] > rightIndex) {
centerIndex = right;
rightIndex = right + longPal[right];
}
}
// Update center and right boundary if needed
if (i + P[i] > right) {
center = i;
right = i + P[i];
}
}

start = (maxCenterIndex - maxPalLength)/2;
end = start + maxPalLength - 1;
string palindrome;
// Find the maximum length and its center
int maxLen = 0, centerIndex = 0;
for (int i = 1; i < n - 1; i++) {
if (P[i] > maxLen) {
maxLen = P[i];
centerIndex = i;
}
}

for(int i=start; i<=end; i++)
palindrome += mainString[i];
return palindrome;
// Extract the longest palindromic substring
int start = (centerIndex - maxLen) / 2; // Get the starting index in the original string
return mainString.substr(start, maxLen);
}

int main(int argc, char *argv[]) {
string mainString, palindrome;
cout << "Enter String:";
cin >> mainString;
palindrome = longestPalindrome(mainString);
cout << "Longest palindrome is: " << palindrome << endl;
}
int main() {
string mainString;
cout << "Enter String: ";
cin >> mainString;

string palindrome = longestPalindrome(mainString);
cout << "Longest palindrome is: " << palindrome << endl;

return 0;
}