Problem - Palindromes!
------------------------
A palindrome is a string that reads the same forward and backward. An
L-palindrome is a string which is a palindrome if you ignore its first
character. An R-palindrome is a palindrome if its last character is
ignored. Given a string, decide whether it is a palindrome, an
L-palindrome, or an R-palindrome.
Input
-----
Each input line contains only a string of at least 1 and at most 25
uppercase letters. End of input is signaled by a line containing only
the word END, which should not be processed.
Output
------
For each input string, output one or more of the following messages:
string is not any type of palindrome.
string is a palindrome.
string is an L-palindrome.
string is an R-palindrome.
where string is the input string. If more than one message
applies, they should be output in the order given above.
Leave a blankline after the output for each input string.
Sample Input
------------
HELLOWORLD
OTTO
OTTOR
OROTOR
TOTO
END
Output for Sample Input
-----------------------
HELLOWORLD is not any type of palindrome.
OTTO is a palindrome.
OTTOR is an R-palindrome.
OROTOR is an L-palindrome.
TOTO is an L-palindrome.
TOTO is an R-palindrome.
Saturday, June 27, 2009
60. Palindromes
Subscribe to:
Post Comments (Atom)

2 comments:
Sol in JAVA:
import java.io.IOException;
class A {
void Begin()
{
String input = ReadLn(255);
while (!input.equals("END"))
{
boolean pal = false;
if (is_palindrome(input))
{
System.out.println(input + " is a palindrome.");
pal = true;
}
if (is_palindrome(input.substring(1,input.length())))
{
System.out.println(input + " is an L-palindrome.");
pal = true;
}
if (is_palindrome(input.substring(0,input.length()-1)))
{
System.out.println(input + " is an R-palindrome.");
pal = true;
}
if (!pal) System.out.println(input + " is not any type of palindrome.");
System.out.println("");
input = ReadLn(255);
}
}
boolean is_palindrome(String s)
{
// This can of course be done with a simple loop
// However, to keep things interesting, here is a
// recursive solution
if (s.length() < 2) return true;
else if (s.charAt(0) != s.charAt(s.length()-1)) return false;
else return is_palindrome(s.substring(1,s.length()-1));
}
static String ReadLn(int maxLg) // utility function to read from stdin
{
byte lin[] = new byte[maxLg];
int lg = 0, car = -1;
String line = "";
try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n'))
break;
lin[lg++] += car;
}
}
catch (IOException e)
{
return (null);
}
if ((car < 0) && (lg == 0))
return (null); // eof
return (new String(lin, 0, lg));
}
public static void main(String args[]) // entry point from OS
{
A myWork = new A(); // create a dynamic instance
myWork.Begin(); // the true entry point
}
}
sol in C++
#include iostream
using namespace std;
// Prototypes
bool test_palindrome(const char *tstr, int len);
// Functions
bool test_palindrome(const char *tstr, int len)
{
bool result;
int idx, limit;
// Default to "palindrome".
result = true;
// Scan letter pairs.
// Ignore the middle letter, if we have an odd number.
limit = len >> 1;
for (idx = 0; idx < limit; idx++)
{
if (tstr[idx] != tstr[len - (idx + 1)])
result = false;
}
// Return the resulting flag.
return result;
}
// Main Program
int main(void)
{
std::string thisline;
const char *charline;
char thischar;
bool done;
bool is_pal;
// While we have input lines, process them.
done = !(cin.good());
while (!done)
{
// Fetch this line.
thisline = "";
thischar = cin.get();
while (cin.good() && ('\n' != thischar))
{
// Input is supposed to be a string of 1-25 capital letters.
// FIXME - Not checking for error cases.
if (isalpha(thischar))
thisline += thischar;
thischar = cin.get();
}
// FIXME - Diagnostics.
//cerr << "--Read \"" << thisline << "\".\n";
// Check for ending cases.
if (!( cin.good() ))
done = true;
else if ( ((std::string) "END") == thisline )
done = true;
// Process this line.
if (!done)
{
charline = thisline.c_str();
is_pal = false;
if (test_palindrome(charline, thisline.length()))
{
cout << thisline << " is a palindrome.\n";
is_pal = true;
}
if (test_palindrome(charline + 1, thisline.length() - 1))
{
cout << thisline << " is an L-palindrome.\n";
is_pal = true;
}
if (test_palindrome(charline, thisline.length() - 1))
{
cout << thisline << " is an R-palindrome.\n";
is_pal = true;
}
if (!is_pal)
cout << thisline << " is not any type of palindrome.\n";
cout << "\n";
}
}
// Report success.
return 0;
}
// This is the end of the file.
Post a Comment