Hi,
Below is the C# code for the PALIN problem - . spoj.pl/problems/PALIN/
It works fine in my machine. It returns NZEC on the SPOJ machine. Can someone please take a look at the code. Is there a way to see the test case, the code is failing, when the exception is thrown?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class PALIN
{
public static void Main(string[] args)
{
int x = int.Parse(Console.ReadLine());
while (x-- > 0)
{
string str = Console.ReadLine();
int val = int.Parse(str);
val = val + 1;
string temp = val.ToString();
if (temp[0] - str[0] == 1)
str = temp;
str = manipulateValues(str);
}
}
static string manipulateValues(string str)
{
if (str.Length % 2 == 0)
{
int middle = str.Length/2;
for (int i = middle - 1,j = middle; i >= 0 || j < str.Length; i--, j++)
{
if (str[i].Equals(str[j]))
{
continue;
}
else if (str[i] > str[j])
{
str = updateValue(str, i, j);
i++;
j--;
}
else
{
str = incrementVal(str, i,j);
i++;
j--;
}
}
Console.Out.WriteLine(str);
}
else
{
int middle = str.Length / 2;
string temp = str;
for (int i = middle - 1, j = middle + 1; i >= 0 || j < str.Length; i--, j++)
{
if (str[i].Equals(str[j]))
{
continue;
}
else if (str[i] > str[j])
{
str = updateValue(str, i, j);
i++;
j--;
}
else
{
str = incrementVal(str, i, j);
i++;
j--;
}
}
if (temp.Equals(str))
str = updateMiddle(str,middle);
Console.WriteLine(str);
}
return null;
}
static string updateMiddle(String str, int middle)
{
StringBuilder builder = new StringBuilder(str);
builder[middle] = (char)(builder[middle] + 1);
return builder.ToString();
}
static string incrementVal(String str, int i, int j)
{
StringBuilder builder = new StringBuilder(str);
builder[i] = (char)(builder[i] + 1);
for (int k = j; k < str.Length; k++)
{
builder[k] = '0';
}
return builder.ToString();
}
static string updateValue(String str, int i, int j)
{
StringBuilder builder = new StringBuilder(str);
builder[j] = (char)builder[i];
return builder.ToString();
}
}
Thank you very much.
-- Bala