Monday 24 October 2016

String sum

The alphabets A...Z are mapped integers 65…90 (ASCII value of A...Z is 65...90) and a...z are mapped to 97…122(ASCII value of a…z is 97...122). The Beautiful function is a function that takes a character as an input and outputs an integer. The integer The Beautiful function returns is the biggest integer in the ASCII representation of the input character.

For example,
If 'A' is the input to the function, the function would return 6 because ASCII of A is 65 and 6 is the biggest among 6 and 5.
If 'C' is the input to the function, the function would return 7 because ASCII of C is 67 and 7 is the biggest among 6 and 7.
If 'x' is the input to the function, the function would return 2 because ASCII of x is 120 and 2 is the biggest among 1, 2 and 0.

Given a string as input, your task is to find out sum of all the values returned by The Beautiful Function.
** The input string may contain numeric values. In that case just add the numeric value. **

Input:
The first line of input will have number of test cases T. T < 10,000
Next T lines will have a string of length L. L < 1,000.

Output:
T lines, each line containing one integer.

Sample Input:
4
abcd
12
a12345b
azAZ09

Sample Output:
28
3
33
35

Explanation for the last test case:
a -> 9
z -> 2
A -> 6
Z -> 9
0 -> 0
9 -> 9
9 + 2 + 6 + 9 + 0 + 9 = 35

String manipulation

Problem Defination:
kriyapad is word present in sentence which shows
actions performed.
Darshil bein fan of kriyapad has prepared list of
kriyapad.
now Anand, Teacher of Darshil gave
assignment to Darshil find maximum number of
kriyapad can be prepared by rearranging given
sentence at a time.

INPUT:

input contain several lines which are kriyapad.
at the end input symbol in new line is shown
by '.' sign.
then there is one line which is number n-
number of sentence given by Anand to Darshil.
then n lines each line contain one sentence.

output:

output contain n lines, each line shows maximum
kriyapad can be formed by rearranging given
sentence

sample Input:
play
give
take
write
speak
listen
.
3
kavan is eating kakdi
Darshil is playing mini-militiae
layte canven wrispeak pgite lis

output:
1
2
6

Wednesday 19 October 2016

PALIN - The Next Palindrome (spoj solution)

A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros.

Input

The first line contains integer t, the number of test cases. Integers K are given in the next t lines.

Output

For each K, output the smallest palindrome larger than K.

Example

Input: 
2
808
2133
Output:
818
2222

Solution in java:


import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    static void p(Object o){ System.out.println(o);}
 public static void main(String[] args)
 {
  Scanner sc = new Scanner(System.in);
  int zz = sc.nextInt();
  for(int z = 0; z < zz;z++)
  {
   String s = sc.next();

   String ans = s.substring(0,s.length()/2);
   if(s.length()%2 == 1)
   {
    ans = ans + s.charAt((s.length()/2))+rev(ans);
   }else
    ans += rev(ans);
   if(larger(ans,s))
   {
    System.out.println(ans);
   }else{
    boolean done = false;
    for(int i = (s.length()-1)/2;i>=0;i--)
    {
     if(ans.charAt(i) != '9')
     {
      done = true;
      if(i == s.length()/2 && s.length()%2 == 1)
      {
       ans = ans.substring(0,s.length()/2)+((char)(ans.charAt(i)+1))+ans.substring(s.length()/2+1);
      }else{
       if(s.length()%2 == 1)
       {
        ans = ans.substring(0,i)+((char)(ans.charAt(i)+1))+zeros(s.length()/2 - i-1);
        ans = ans+"0"+rev(ans);
       }else{
        ans = ans.substring(0,i)+((char)(ans.charAt(i)+1))+zeros(s.length()/2 - i-1);
        ans = ans+rev(ans);
       }
      }
      break;
     }
    }
    if(done) System.out.println(ans);
    else
    {
     ans = "1";
     while(ans.length() < s.length())
      ans +="0";
     ans += "1";
     System.out.println(ans);
    }
   }
  }
 }

 private static String zeros(int i) {
  StringBuilder z = new StringBuilder();
  for(int j = 0; j < i;j++)
   z.append("0");
  return z.toString();
 }
 private static boolean larger(String ans, String s) {
  for(int i = 0; i < ans.length();i++)
  {
   char a = ans.charAt(i);
   char b = s.charAt(i);
   if(a < b) return false;
   if(a > b) return true;
  }
  return false;
 }

 private static String rev(String ans) {
  StringBuilder st = new StringBuilder(ans);
  return st.reverse().toString();
 }
}