fork download
  1. import java.io.OutputStreamWriter;
  2. import java.io.BufferedWriter;
  3. import java.io.OutputStream;
  4. import java.io.PrintWriter;
  5. import java.util.Random;
  6. import java.io.Writer;
  7. import java.util.List;
  8. import java.io.IOException;
  9. import java.util.InputMismatchException;
  10. import java.util.ArrayList;
  11. import java.math.BigInteger;
  12. import java.io.InputStream;
  13. import java.io.*;
  14.  
  15. /**
  16.  * Built using CHelper plug-in
  17.  * Actual solution is at the top
  18.  * @author sandeepandey
  19.  */
  20. public class Main {
  21.  
  22. public static void main(String[] args) {
  23. OutputStream outputStream = System.out;
  24. OutputWriter out = new OutputWriter(outputStream);
  25. BufferedReader in=null;
  26. try {
  27. ExtendToPalindrome solver = new ExtendToPalindrome();
  28. //int testCount = Integer.parseInt(in.next());
  29. solver.solve(0, in, out);
  30.  
  31. } catch(Exception e) {
  32.  
  33. }
  34. //out.close();
  35. }
  36. }
  37.  
  38. class ExtendToPalindrome {
  39.  
  40. private String input ;
  41. public void solve(int testNumber, BufferedReader in, OutputWriter out) throws IOException {
  42. while((input= in.readLine()) != null) {
  43. int lenCount = input.length();
  44.  
  45. StringHash hashRef = new ConcreteHasher(input);
  46. int longestSuffix = 0;
  47. for(int i = 0;i < lenCount ; i++) {
  48.  
  49. long fHash = hashRef.hash(i,lenCount-1);
  50. long rHash = hashRef.reverseHash(lenCount-1,i);
  51.  
  52. if(fHash == rHash) {
  53. longestSuffix = i;
  54. break;
  55. }
  56. }
  57. //out.printLine("Longest Suffix::"+longestSuffix);
  58. String outputString = input + StringUtils.reverse(input.substring(0,longestSuffix));
  59. out.printLine(outputString);
  60. }
  61.  
  62. }
  63. }
  64.  
  65. class InputReader {
  66.  
  67. private InputStream stream;
  68.  
  69. private byte[] buf = new byte[1024];
  70.  
  71. private int curChar;
  72.  
  73. private int numChars;
  74.  
  75. private SpaceCharFilter filter;
  76. public InputReader(InputStream stream) {
  77.  
  78. this.stream = stream;
  79.  
  80. }
  81.  
  82.  
  83.  
  84. public int read() {
  85.  
  86. if (numChars == -1)
  87.  
  88. throw new InputMismatchException();
  89.  
  90. if (curChar >= numChars) {
  91.  
  92. curChar = 0;
  93.  
  94. try {
  95.  
  96. numChars = stream.read(buf);
  97.  
  98. } catch (IOException e) {
  99.  
  100. throw new InputMismatchException();
  101.  
  102. }
  103.  
  104. if (numChars <= 0)
  105.  
  106. return -1;
  107.  
  108. }
  109.  
  110. return buf[curChar++];
  111.  
  112. }
  113.  
  114.  
  115.  
  116. public String readString() {
  117.  
  118. int c = read();
  119.  
  120. while (isSpaceChar(c))
  121.  
  122. c = read();
  123.  
  124.  
  125. do {
  126.  
  127. res.appendCodePoint(c);
  128.  
  129. c = read();
  130.  
  131. } while (!isSpaceChar(c));
  132.  
  133. return res.toString();
  134.  
  135. }
  136.  
  137.  
  138.  
  139. public boolean isSpaceChar(int c) {
  140.  
  141. if (filter != null)
  142.  
  143. return filter.isSpaceChar(c);
  144.  
  145. return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  146.  
  147. }
  148.  
  149.  
  150.  
  151. public String next() {
  152.  
  153. return readString();
  154.  
  155. }
  156.  
  157.  
  158.  
  159. public interface SpaceCharFilter {
  160.  
  161. public boolean isSpaceChar(int ch);
  162.  
  163. }
  164.  
  165. }
  166.  
  167. class OutputWriter {
  168.  
  169. private final PrintWriter writer;
  170. public OutputWriter(OutputStream outputStream) {
  171.  
  172. writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
  173.  
  174. }
  175.  
  176.  
  177.  
  178. public OutputWriter(Writer writer) {
  179. this.writer = new PrintWriter(writer);
  180. }
  181.  
  182.  
  183.  
  184. public void print(Object...objects) {
  185.  
  186. for (int i = 0; i < objects.length; i++) {
  187.  
  188. if (i != 0)
  189.  
  190. writer.print(' ');
  191.  
  192. writer.print(objects[i]);
  193.  
  194. }
  195.  
  196. }
  197.  
  198.  
  199.  
  200. public void printLine(Object...objects) {
  201.  
  202. print(objects);
  203.  
  204. writer.println();
  205.  
  206. }
  207.  
  208.  
  209.  
  210. public void close() {
  211.  
  212. writer.close();
  213.  
  214. }
  215.  
  216.  
  217.  
  218. }
  219.  
  220. interface StringHash {
  221.  
  222. public long hash(int from,int to);
  223. public long reverseHash(int from ,int to);
  224. }
  225.  
  226. class ConcreteHasher extends SimpleStringHash {
  227.  
  228. public ConcreteHasher(String input) {
  229. super(input);
  230. }
  231. public long hashFunction(long previousHash, char currentChar, int subtractor, long multiplier) {
  232. return (previousHash * multiplier + currentChar-subtractor);
  233. }
  234.  
  235. }
  236.  
  237. class StringUtils {
  238.  
  239. public static String reverse(String sample) {
  240. StringBuilder result = new StringBuilder(sample);
  241. result.reverse();
  242. return result.toString();
  243.  
  244. }
  245.  
  246. //----------------------------------------------------------------------
  247.  
  248. }
  249.  
  250. abstract class SimpleStringHash extends AbstractStringHash {
  251.  
  252. private static long[] hashArray = new long[0];
  253. private static long[] baseArray = new long[0];
  254. private static long[] reverseHashArray = new long[0];
  255. private static int length;
  256. public SimpleStringHash(CharSequence input) {
  257. super();
  258. length = input.length();
  259. hashArray = new long[length + 1];
  260.  
  261. baseArray = new long[length + 1];
  262. reverseHashArray = new long[length + 1];
  263. long tmpHash = 0;
  264. long tmpMul = 1;
  265.  
  266. for(int i = 0; i < length ; i++) {
  267. baseArray[i] = tmpMul;
  268. tmpMul = tmpMul * DEFAULT_MAULTIPLIER;
  269. }
  270.  
  271. for(int i = 0; i < length ; i++) {
  272. tmpHash = hashFunction(tmpHash,input.charAt(i),48,DEFAULT_MAULTIPLIER);
  273. hashArray[i] = tmpHash;
  274. }
  275.  
  276. tmpHash = 0;
  277. for(int i = length-1 ; i >=0 ; i--) {
  278. tmpHash = hashFunction(tmpHash,input.charAt(i),48,DEFAULT_MAULTIPLIER);
  279. reverseHashArray[i] = tmpHash;
  280. }
  281. }
  282.  
  283.  
  284. public abstract long hashFunction(long previousHash,char currentChar,int subtractor, long multiplier);
  285.  
  286. public long hash(int from, int to) {
  287.  
  288. int windowSize = to-from + 1;
  289. return (hashArray[to]-((from <=0) ? 0 : hashArray[from-1] * baseArray[windowSize]));
  290. }
  291.  
  292.  
  293. public long reverseHash(int from, int to) {
  294.  
  295. int windowSize = from-to + 1;
  296. long hashToReturn = reverseHashArray[to]- ((from == length-1) ? 0 : reverseHashArray[from+1] * baseArray[windowSize]);
  297. return hashToReturn;
  298. }
  299.  
  300.  
  301.  
  302. }
  303.  
  304. abstract class AbstractStringHash implements StringHash {
  305.  
  306. protected final long DEFAULT_MAULTIPLIER ;
  307. public AbstractStringHash() {
  308. Random randomBehaviour = new Random(547315431513L + System.currentTimeMillis()) ;
  309. int randomNumber = randomBehaviour.nextInt(Integer.MAX_VALUE);
  310. randomNumber = IntegerUtils.nextPrime(randomNumber);
  311. DEFAULT_MAULTIPLIER = 1000004933;
  312. }
  313.  
  314.  
  315. }
  316.  
  317. class IntegerUtils {
  318.  
  319. public static int nextPrime(int currentValue) {
  320. BigInteger currentNum = new BigInteger(String.valueOf(currentValue));
  321. while(!(currentNum.isProbablePrime(2))) {
  322.  
  323. currentNum=currentNum.add(BigInteger.ONE);
  324. }
  325. return Integer.valueOf(currentNum+"");
  326. }
  327.  
  328.  
  329. }
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
Standard output is empty