ACM: Web Navigation - c++

Note:This problem I wrote is just for the people who know about ACM questions.
I have problem with this question. I wrote a good solution for this but every time I send it I get Wrong Answer. I don't know what's wrong here. I test this code for lots of test cases. Can you help me to fix my code?
Here is the link of the question: http://sharecode.ir/section/problemset/problem/1022.
Here is my code :
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
int n;
cin >> n;
while (n--)
{
string page[1000] = { "" };
int cntr = 0;
page[cntr] = "http://www.acm.org/";
string page1;
while (1)
{
cin >> page1;
if (page1 == "QUIT")
break;
if (page1 == "VISIT")
{
cntr++;
cin >> page1;
page[cntr] = page1;
cout << page1 << endl;
}
if (page1 == "BACK")
{
cntr--;
if (cntr >= 0)
cout << page[cntr] << endl;
else
{
cout << "Ignored" << endl;
cntr = 0;
}
}
if (page1 == "FORWARD")
{
cntr++;
if (page[cntr] == "")
cout << "Ignored" << endl;
else
cout << page[cntr] << endl;
}
}
if (n) cout << endl;
}
}

You don't clear forwards after visiting a site. That's why you get wrong answer.
Here is the correct code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(int n){
cin >> n;
while (n--){
string c;
vector <string> a(999,"\0");
a[0] = "http://www.acm.org/";
int i = 0;
while(cin>>c,c != "QUIT"){
if (c == "VISIT"){
i++;
string s;
cin >> s;
a[i] = s;
cout << a[i] << "\n";
int t = i+1;
while (a[t] != "\0") {
a[t] = "\0";
t++;
}
}
if (c == "BACK"){
i--;
if (i < 0) {cout << "Ignored\n"; i=0;} else cout << a[i] << "\n";
}
if (c == "FORWARD"){
i++;
if (a[i] == "\0") {cout << "Ignored\n"; i--;} else cout << a[i] << "\n";
}
}
if (n) cout << "\n";
}
}

Related

Why is my code stopping prematurely? what have i done wrong?

I'm just starting so I'm trying to write a program which determine if a number is positive or negative.
#include <iostream>;
int step_function(int x) {
int result = 0;
if (x > 0)
result = 1;
else if (x < 0)
result = -1;
else
result = 0;
return result;
}
using namespace std;
int main() {
int num;
cout<< "please enter number : ";
cin >> num;
int a = step_function(num);
if (a == 1)
printf("%d is positive", num);
else if (a == -1)
printf("%d is negative", num);
else
printf(" it is zero");
return 0;
}
There is a few things you should do:
First things first you should get yourself a Good Book for C++.
Second thing is read why using namespace std; is a bad idea.
Lastly here is your code fixed. You needed to remove the semicolon as well as removing the printf(). I also removed the using namespace std; which made it more readable.
#include <iostream>
int step_function(int); //Function prototype
int main() {
int num;
std::cout << "please enter number : ";
std::cin >> num;
int a = step_function(num);
if (a == 1)
std::cout << num << " is postive";
else if (a == -1)
std::cout << num << " is negative";
else std::cout <<" it is zero";
return 0;
}
int step_function(int x)
{
int result = 0;
if (x > 0) result = 1;
else if (x < 0) result = -1;
else result = 0;
return result;
}
Don't use semicolon after #include <iostream>.
I think for C++ cout is more standard whereas printf is from C.
You can also include printing of the text in the step_function. Also, it's better to use braces {} after if and else statements for clarity especially if the code becomes complex.
#include <iostream>
using namespace std;
void step_function(int x) {
if (x > 0) {
cout << x << " is positive" << endl;
}
else if (x < 0) {
cout << x << " is negative" << endl;
}
else {
cout << "it is zero" << endl;
}
}
int main() {
int num;
cout<< "please enter number : ";
cin >> num;
step_function(num);
return 0;
}

what to do so that I don't cut the string after the space [duplicate]

This question already has answers here:
Why std::cin string input asking me for each space
(2 answers)
Closed 2 years ago.
I wrote a program that encrypts a string with the Caesar method. I have a problem when I try to enter a password consisting of several words separated by a space. the code itself works but cuts me off at the time of a space in the string
i tried to add some ifs but it didn't have any effect, so it only shows working code
#include <fstream>
#include <cstdlib>
#include <string>
#include <conio.h>
using namespace std;
int main() {
string wynik = "";
int i,przesu;
system("cls");
cout << "how many shift a password" << endl;
cin >> przesu;
cout << "your password to encode: ";
cin >> wynik;
for (i = 0; i < wynik.length(); i++) {
if ((int)wynik[i] == 32 ) {
wynik[i] = wynik[i];
}
else {
wynik[i] = toupper(wynik[i]);
}
}
for (i = 0; i < wynik.length(); i++) {
if (isupper(wynik[i])) {
wynik[i] = wynik[i] + przesu;
if (wynik[i] > 90) {
wynik[i] = wynik[i] - 26;
}
}
else {
wynik[i] += wynik[i];
}
}
cout << endl << "your encode password: " << wynik << endl;
cout << "press any button to continue";
_getch();
}
Hi I think that you could use the getline() function for this. It reads the line untill it encounters a delimiter(the end of line character "\n" by default). But you should also use cin.ignore() because without it your getline() function wouldn't work properly.
Here is your code with the getline():
#include <fstream>
#include <cstdlib>
#include <string>
#include <conio.h>
#include <iostream>
using namespace std;
int main() {
string wynik = "";
int i,przesu;
system("cls");
cout << "how many shift a password" << endl;
cin >> przesu;
cin.ignore();
cout << "your password to encode: ";
getline(cin, wynik);
for (i = 0; i < wynik.length(); i++) {
if ((int)wynik[i] == 32 ) {
wynik[i] = wynik[i];
}
else {
wynik[i] = toupper(wynik[i]);
}
}
for (i = 0; i < wynik.length(); i++) {
if(wynik[i] != ' '){
if (isupper(wynik[i])) {
wynik[i] = wynik[i] + przesu;
if (wynik[i] > 90) {
wynik[i] = wynik[i] - 26;
}
}
else {
wynik[i] += wynik[i];
}
}
}
cout << endl << "your encode password: " << wynik << endl;
cout << "press any button to continue";
_getch();
}
I also added another if statement in your second loop. It checks if the character is different than a space because we do not want any random symbols in our output. I hope that I answered your question. Let me know if you want to know anything else.
#include <iostream>
using namespace std;
int main() {
string end_wynik = "",wynik;
int i,przesu;
system("cls");
cout << "how many shift a password" << endl;
cin >> przesu;
cout << "your password to encode: ";
while(cin >> wynik) {
for (i = 0; i < wynik.length(); i++) {
wynik[i] = toupper(wynik[i]);
if (isupper(wynik[i])) {
wynik[i] = wynik[i] + przesu;
if (wynik[i] > 90) {
wynik[i] = wynik[i] - 26;
}
}
else {
wynik[i] += wynik[i];
}
}
end_wynik += wynik + " ";
}
cout << endl << "your encode password: " << end_wynik << endl;
system("pause");
}
Example here IDEONE

Is there any way to simplify this code? & would it be better to insert a bool like the example code I show?

This is my code, which chooses a random number from 0 to 10 for the user to guess.
//guess the number game
//my code
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
unsigned int secretNumber;
int guess;
int maxNumber = 10;
int maxTries = 4;
int numTries = 1;
srand(static_cast<unsigned int>(time(0)));
secretNumber = (rand() % 10)+ 1;
cout << "GUESS A NUMBER FROM 0 TO 10!!\n";
do {
cout << "\nGuess: \n";
cin >> guess;
if (guess < secretNumber)
{
cout << "too low:(:(!! ";
numTries++;
cout << "Guesses Left: " << maxTries - numTries;
} ***//Would it be better to add a bool in the condition?***
else if (guess > secretNumber && guess <= maxNumber)
{
cout << "Too high:D:D!! ";
numTries++;
cout << "Guesses Left: " << maxTries - numTries;
}
else if (guess > maxNumber)
{
cout << "Do you know how to count to 10?\n";
cout << "Only from 0 TO 10!! ";
numTries++;
cout << "Guesses Left: " << maxTries - numTries;
}
else {
cout << "WOW! you GUESSED IT?! AMAZING!!!!";
cout << "You're right! the number is " << guess;
cout << "\nYou got it right in " << numTries << " guesses!!!";
}
if (numTries == maxTries)
{
cout << "\n\nYou LOOSE :( LOL!";
}
} while (guess != secretNumber && maxTries != numTries);
return 0;
}
This is the teacher's code, which is simpler and includes a bool variable.
Should my previous code be simpler, just as this one?
// Example program
// Teacher's code
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int secretNumber = 7;
int guess;
int numTries = 0;
int maxTries = 3;
bool outOfGuesses = false;
while (secretNumber != guess && !outOfGuesses)
{
if (numTries != maxTries)
{cout << "Guess a Number: ";
cin >> guess;
numTries++;}
else
{
outOfGuesses = true;
}
}
if (outOfGuesses)
{
cout << "You loose!";
}
else
{
cout << "You win!";
}
return 0;
}
//Is my code as efficient and simple as the teacher's code?
//Is there a simpler way to do what I intended to do in my code?

Recursive nested algorithm

I am trying to create an algorithm for bruteForce cracking MD5 hash.
My goal is to measure the time consumption when splitting into fibers for the processor and optionally graphics in compute clastr.
I got stuck in creating an algorithm.
The input should be a string. According to the number of string characters, I need to create the same number of forcycles.
Statically written for 3 digist, it looks like this:
#include <iostream>
#include <string>
#include "md5.h"
using namespace std;
int main()
{
string imput = "slv";
cout << "imput string: "<< imput << endl;
cout << "MD5 HASH: "<< wantedHash << endl;
do
{
cout << '\n' << "Enable BruteForce Craker";
} while (cin.get() != '\n');
string s;
for(int i=0; i != 256; i++)
{
for(int j=0; j != 256; j++)
{
for(int k=0; k != 256; k++)
{
string s = md5(string(1,(char)i) + string(1,(char)j) + string(1,(char)k));
serchCounter++;
if(s == wantedHash)
{
cout << "Find: " << string(1,(char)i) + string(1,(char)j) + string(1,(char)k) << endl;
cout << "Count TestedHash: " << serchCounter << endl;
return 0;
}
}
}
}
return 0;
}
My idea .. something like that ...
#include <iostream>
#include <string>
#include "md5.h"
using namespace std;
string imput = "s";
string wantedHash = md5(imput);
double serchCounter = 0;
int bruteForse(int longString, string s)
{
for(int i=0; i != 256; i++)
{
string s = md5(string(1,(char)i));
serchCounter++;
if(s == wantedHash)
{
cout << "Find: " << string(1,(char)i);
cout << "Count TestedHash: " << serchCounter << endl;
return 0;
}
}
if(longString > 1) bruteForse(--longString, s);
return 0;
}
int main()
{
cout << "imput string: "<< imput << endl;
cout << "MD5 HASH: "<< wantedHash << endl;
bruteForse(imput.length(),imput);
}
I would do:
bool increase(std::string& s)
{
for (auto rit = s.rbegin(); rit != s.rend(); ++rit) {
auto& c = s[i];
if (c == -1) {
c = 0;
continue;
} else if (c == 127) {
c = -128;
} else {
++c;
}
return true;
}
return false;
}
void bruteForce(std::size_t size, const string& wantedHash)
{
std::string s;
s.resize(size);
do {
if (md5(s) == wantedHash) {
cout << "Find: " << s << std::endl;
}
} while (increase(s));
}

How to display my Queue & Stack?

I Wanna display my EvenQueue, EvenStack, OddQueue & EvenQueue? I already try some methods but the compiler gave me some errors.
Any help would be appreciated... Or any tips.
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
int main()
{
stack<int> OddStack;
queue<int> OddQueue;
stack<int> EvenStack;
queue<int> EvenQueue;
int MyNumbers[10];
int InNum;
for(int i = 0; i < 10; i++)
{
cout << "Enter Number " << i << ": ";
cin >> InNum;
MyNumbers[i] = InNum;
if(InNum % 2 == 0)
{
EvenQueue.push(InNum);
EvenStack.push(InNum);
}
else
{
OddQueue.push(InNum);
OddStack.push(InNum);
}
}
cout << "Stack" << "\t\t" << "Queue" << endl;
return 0;
}
Assign it to tempstack
stack<int>tempStack = OddStack
and start poping from it and see what is inside
while(tempStack.empty() == false){
int x = tempStack.top();
cout << x << endl;
tempStack.pop();
}
same goes for the queue