fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. String s = "LCCT";
  14. System.out.println("LCT count after one insertion is "+ lctCount(s));
  15. }
  16. static int lctCount(String s){
  17. int n = s.length();
  18. long[] prefixL = new long[n];
  19. long[]prefixLC = new long[n];
  20. long l = 0, lc = 0, lctCount = 0;
  21. for(int i=0;i<n;i++){
  22. char c = s.charAt(i);
  23. if(c == 'L'){
  24. l++;
  25. }
  26. if(c == 'C'){
  27. lc+=l;
  28. }
  29. if(c == 'T'){
  30. lctCount += lc;
  31. }
  32. prefixL[i] = l;
  33. prefixLC[i] = lc;
  34. }
  35. long[]sufixT = new long[n];
  36. long[]sufixTC = new long[n];
  37. long t = 0,tc = 0;
  38. for(int i=n-1;i>=0;i--){
  39. char c = s.charAt(i);
  40. if( c == 'T'){
  41.  
  42. t++;
  43. }
  44. if( c == 'C'){
  45. tc+=t;
  46. }
  47. sufixT[i] = t;
  48. sufixTC[i] = tc;
  49. }
  50. long answer1 = lctCount+sufixTC[0];
  51. long answer2 = lctCount+prefixLC[n-1];
  52. long answer3 = 0;
  53. for(int i=0;i<n-1;i++){
  54. answer3 = Math.max(answer3, prefixLC[i]*sufixT[i+1]);
  55. }
  56. answer3 = Math.max(answer3,Math.max(answer1,answer2));
  57. return (int)answer3;
  58. }
  59. }
Success #stdin #stdout 0.1s 55488KB
stdin
Standard input is empty
stdout
LCT count after one insertion is 4