401_Please help me and tell me where is the wrong?

All about problems in Volume IV. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

401_Please help me and tell me where is the wrong?

Postby i see » Fri Jun 01, 2012 4:02 pm

[*]I have tried again and again,but i can't get ac all the time!All though it's easy,please help me ! thanks in advance.
Code: Select all
#include <iostream>
#include <fstream>
#include <string>
//#define LOCAL
#define fin cin
#define fout cout
using namespace std;

bool is_palindrome_string(const string &s)
{
     for(int i=0;i<=s.length()-i-1;++i)
         if(s[i]!=s[s.length()-i-1])  return false;
     return true;   
     }
     
bool is_mirrored_string(const string &s)
{
     
     bool log;
     string ss[23]={"E3","JL","LJ","S2","Z5","2S","3E","5Z","AA","HH","II","OO","TT","UU","VV","XX","WW","YY","11","88","MM"};
     for(int i=s.length()-1;i>=0;--i)
         {
          log=false;
          for(int j=0;j<21;++j)
             if(ss[j][0]==s[i])
               {log=true;
                if(s[s.length()-i-1]!=ss[j][1]) return false ;
                }
          if(!log) return false;
          }
     return true;
     }

int main()
{
    #ifdef LOCAL
    ifstream fin("in.cpp");
    ofstream fout("out.cpp");
    #endif
    string s;
    while(fin>>s)
    {
      if(is_palindrome_string(s)) {
             if(is_mirrored_string(s))  fout<< s<<" -- is a mirrored palindrome."<<endl;
             else fout<<s<<" -- is a regular palindrome." <<endl;
             }
      else  {
            if(is_mirrored_string(s)) fout<<s<<" -- is a mirrored string." <<endl;
            else fout<<s<<" -- is not a palindrome." <<endl;
      }
      fout<<endl;   
    }
   
    return 0;
}

i see
New poster
 
Posts: 9
Joined: Sat May 19, 2012 7:35 am

Re: 401_Please help me and tell me where is the wrong?

Postby brianfry713 » Fri Jun 01, 2012 11:38 pm

Your code doesn't work for strings of length 1.

s.length() returns a size_t unsigned integer. So taking s.length()-i-1 could give a negative result, causing the limit of your for loop i<=s.length()-i-1 to not give the results you expected.
brianfry713
Guru
 
Posts: 1751
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 401_Please help me and tell me where is the wrong?

Postby i see » Sat Jun 02, 2012 9:06 am

Hi, brianfry713 , I want to thank you for making me understand the error where i did't find all the time , i have passed the problem,thank you again !
i see
New poster
 
Posts: 9
Joined: Sat May 19, 2012 7:35 am

Re: 401_Please help me and tell me where is the wrong?

Postby mahade hasan » Sun Jul 01, 2012 10:28 am

cut>>>>After aCC
Last edited by mahade hasan on Mon Aug 06, 2012 12:46 am, edited 1 time in total.
we r surrounded by happiness
need eyes to feel it!
User avatar
mahade hasan
Learning poster
 
Posts: 62
Joined: Thu Dec 15, 2011 3:08 pm

Re: 401_Please help me and tell me where is the wrong?

Postby brianfry713 » Mon Jul 02, 2012 9:45 pm

AE -- is not a palindrome.
brianfry713
Guru
 
Posts: 1751
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA


Return to Volume IV

Who is online

Users browsing this forum: No registered users and 0 guests