Sign in to follow this  
iamcreasy

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by iamcreasy
yeah...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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by iamcreasy
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.

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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Buckeye
Did you #include <fstream>?

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 this post


Link to post
Share on other sites
Quote:
Original post by mattd
Quote:
Original post by iamcreasy
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.

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 this post


Link to post
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,.9`12345678 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 this post


Link to post
Share on other sites
Quote:
Original post by Washu
You 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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Washu
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?


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

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this