# 10082

## Recommended Posts

m trying to solve ACM - 10082 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=31&page=show_problem&problem=1023 Judge is responding wrong answer...but, i have no idea why. Any suggestion?
#include<iostream>
#include<string>
using namespace std;

char change(char x);

int main()
{
string str1;

getline(cin, str1, '\n');

transform(str1.begin(), str1.end(), str1.begin(), change);
cout << str1;

return 0;
}
char change(char x)
{
//---first row----------
if(x == '1') return 96;
if(x == '2') return '1';
if(x == '3') return '2';
if(x == '4') return '3';
if(x == '5') return '4';

if(x == '6') return '5';
if(x == '7') return '6';
if(x == '8') return '7';

if(x == '9') return '8';
if(x == '0') return '9';
if(x == '-') return '0';

if(x == '=') return '-';

//----2nd row------------

if(x == 'Q') return 9;
if(x == 'W') return 'Q';
if(x == 'E') return 'W';

if(x == 'R') return 'E';
if(x == 'T') return 'R';
if(x == 'Y') return 'T';

if(x == 'U') return 'Y';
if(x == 'I') return 'U';
if(x == 'O') return 'I';

if(x == 'P') return 'O';
if(x== '[') return 'P';
if(x== ']') return '[';
if(x== '\\') return ']';

//----3rd row------------

if(x== 'S') return 'A';
if(x== 'D') return 'S';
if(x== 'F') return 'D';

if(x== 'G') return 'F';
if(x== 'H') return 'G';
if(x== 'J') return 'H';

if(x== 'K') return 'J';
if(x== 'L') return 'K';
if(x== '\;') return 'L';
if(x== '\'') return '\;';

//---4th row-------------

if(x== 'X') return 'Z';
if(x== 'C') return 'X';
if(x== 'V') return 'C';

if(x== 'B') return 'V';
if(x== 'N') return 'B';
if(x== 'M') return 'N';

if(x== ',') return 'M';
if(x== '.') return ',';
if(x== '/') return '.';

if(x == ' ') return ' ';
}


one more thing, is there any way to get ride of this if-return repeatation.

##### Share on other sites
Are you handling the multiple lines of input?

You could replace the huge if-else bit with a switch statement, or better still, use a std::string for each row. You can then find each character, and use the one to the left of it.

##### Share on other sites
Thanks...

yes, it is not taking several line...But how do i take several line in this manner?

Quote:
 use the one to the left of it.

How would i know which ones are adjacent.There is no pattern.And isn't using switch is almost the same as this if-return.It will also take one statement per character.

##### Share on other sites
I think what mattd was suggesting is something like (off the top of my head):
char tmp[2];tmp[0]=x;tmp[1]=0;string inStr(tmp);string keyStr = "~1234567890";int pos = keyStr.find(inStr);if( pos != string::npos && pos > 0) return keyStr[pos-1];// don't know what you do with '~'... // and so on for other rows... // keyStr can also be a vector of string or just a string of the entire// keyboard, I suppose

##### Share on other sites
yeah...interesting.

But, im still wondering, how to take multiple line as input, while using string class?

##### Share on other sites
Quote:
 Original post by iamcreasyyeah...interesting.But, im still wondering, how to take multiple line as input, while using string class?

#include <iostream>#include <string>using namespace std;string process(const string& str) {    return "Processed: " + str;}int main() {    while(cin) {        string line;        getline(cin, line);        cout << process(line) << "\n";    }}

##### Share on other sites
Thanks a lot...the problem is almost solved...but a little problem has appeared.

The following code is supposed to print three lines from "in.txt" file(which contains three lines).But it is printing the last line twice.The size method of vector is also showing one extra count of the total line amount.

s_v[3] and s_v[4] is showing the same result.where s_v[4] dont even exists in "in.txt"

How am i supposed to get ride of this problem?

#include<iostream>#include<vector>#include<string>using namespace std;int main(){    freopen("in.txt", "r", stdin);    vector<string> s_v;    string str;    while(cin)    {        getline(cin, str);        s_v.push_back(str);    }    for(int i=0; i<s_v.size(); i++)    {        cout << s_v[i] << endl;    }    return 0;}

also, i wanna get ride of this line

    freopen("in.txt", "r", stdin);

I used it when i coded in C.What should i use in C++?

##### Share on other sites
Quote:
 Original post by iamcreasyThanks a lot...the problem is almost solved...but a little problem has appeared.The following code is supposed to print three lines from "in.txt" file(which contains three lines).But it is printing the last line twice.The size method of vector is also showing one extra count of the total line amount.

Oops... use while(cin.good()), not while(cin).

Quote:
 also, i wanna get ride of this line  freopen("in.txt", "r", stdin);I used it when i coded in C.What should i use in C++?

Something like:
ifstream file("in.txt");cin.rdbuf(file.rdbuf());

##### Share on other sites
This line is generating the following error..

ifstream file("in.txt");

error: variable std::ifstream file' has initializer but incomplete type

:(

##### Share on other sites
Did you #include <fstream>?

Also, it seems your overcomplicating it a bit.
ifstream f("in.txt");string str;for(int i=0; i<3; i++){   getline(f,str,'\n');   cout << str.c_str() << endl;}

Am I missing something?

##### Share on other sites
Quote:
 Original post by BuckeyeDid you #include ?Also, it seems your overcomplicating it a bit....for(int i=0; i<3; i++)...

But, the amount of line is not fixed. :)
That's why, i am taking the size of the vector.

Finally...a decent code.. :D

// ACM problem 10082 - WERTYU#include<iostream>#include<string>using namespace std;int main(){    string str, final_str;    string data_str("1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./");    while(cin.good())    {        getline(cin, str);        for(unsigned int i=0; i<str.size(); i++)            final_str += data_str[data_str.find(str[i]) - 1];        cout << final_str << endl;        final_str.clear();    }    return 0;}

BUT, why they are not still accepting my code.I have checked character by character....didn't found any error....grrrr.

pain!

##### Share on other sites
Quote:
Original post by mattd
Quote:
 Original post by iamcreasyThanks a lot...the problem is almost solved...but a little problem has appeared.The following code is supposed to print three lines from "in.txt" file(which contains three lines).But it is printing the last line twice.The size method of vector is also showing one extra count of the total line amount.

Oops... use while(cin.good()), not while(cin).

can u plz explain this to me, why cin.good() works?

cause, if error occurs at getline another push_back will still take place...isn't it?If it is, then it is not supposed to work!

##### Share on other sites
You forgot to handle space...
A C++0x version (note for_each is not const based...so modification of the underlying type is possible):
#include <iostream>#include <string>#include <algorithm>int main() {    char dec[] = {"       ;    M0,.912345678 L -    VXSWDFGUHJKNBIO EARYCQZT P]["};    std::string line;    while(std::getline(std::cin, line)) {        std::for_each(line.begin(), line.end(), [&](char& c) {c = dec[c - 32];});        std::cout<<line<<std::endl;    }}

[Edited by - Washu on February 18, 2010 12:10:45 AM]

##### Share on other sites
Quote:
 Original post by WashuYou forgot to handle space...

The program somehow handle space on it's own.... :O

i dont know how..but i added space handling part.But still the code is not being accepted.

#include<iostream>#include<algorithm>#include<fstream>#include<string>using namespace std;int main(){  // ifstream file("in.txt");  //  cin.rdbuf(file.rdbuf());    string str, final_str;    string data_str("1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./");    while(cin.good())    {        getline(cin, str);        for(unsigned int i=0; i<str.size(); i++)        if(str[i] != ' ')        {         if(data_str.find(str[i]) != string::npos)            final_str += data_str[data_str.find(str[i]) - 1];        }        else            final_str += str[i];        cout << final_str << endl;        final_str.clear();    }    return 0;}

Quote:
 A C++0x version (note for_each is not const based...so modification of the underlying type is possible):

I dont understand the for_each line. :(
Moreover i failed to execute the program.
It shows, ";" missing, "c" undeclared and syntax error ")".

What does it mean by "[&]" ?

##### Share on other sites
Your spaces may have been coming up in the output as "blanks" but they weren't "spaces", you were quite lucky actually most likely.

As for my sample code, that's using C++0x, which their compiler doesn't have support for, nevertheless: the solution is correct and if you can figure out how to parse it, you may find a solution yourself. That is the point of the ACM challenges.

Have you validated that it is producing the appropriate output, with a large set of test cases?

##### Share on other sites
Quote:
 Original post by WashuYour spaces may have been coming up in the output as "blanks" but they weren't "spaces", you were quite lucky actually most likely.As for my sample code, that's using C++0x, which their compiler doesn't have support for, nevertheless: the solution is correct and if you can figure out how to parse it, you may find a solution yourself. That is the point of the ACM challenges.Have you validated that it is producing the appropriate output, with a large set of test cases?

yeah..ive tried with large set of test data.But, but there was no error.

As for your code, I can't decipher it.. :(

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628301
• Total Posts
2981906

• 9
• 11
• 11
• 10
• 10