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 StringManupulation {
  9.  
  10. public static void main(String []args) throws IOException
  11. {
  12. System.out.println("Text:");
  13. String text = br.readLine();
  14.  
  15. System.out.println("Prefix:");
  16. String prefix = br.readLine();
  17. System.out.println("Suffix:");
  18. String suffix = br.readLine();
  19.  
  20. System.out.println("output:"+calculateScore(text, prefix, suffix));
  21.  
  22. }
  23.  
  24. static String calculateScore(String text, String prefix, String suffix) {
  25.  
  26. int n = text.length();
  27. int sl = suffix.length();
  28. int pl = prefix.length();
  29.  
  30.  
  31. Map<String, Integer> map = new HashMap<>();
  32.  
  33. for(int i =0; i<n; i++) {
  34. for(int j = i+1; j<=n; j++) {
  35.  
  36. String sub = text.substring(i,j);
  37.  
  38. int ps = 0, ss = 0, subL = sub.length();
  39.  
  40. for(int k =0; k < subL; k++) {
  41. if(subL - k <= sl) {
  42. if(sub.substring(k).equals(suffix.substring(0,subL-k))) {
  43. ps = Math.max(subL-k, ps);
  44. }
  45. }
  46. }
  47.  
  48. for(int k =0; k < subL; k++) {
  49. if(k < pl) {
  50. if(sub.substring(0,k+1).equals(prefix.substring(pl-k-1, pl))) {
  51. ss = Math.max(k+1,ss);
  52. }
  53. }
  54. }
  55.  
  56. if(map.get(sub)==null) {
  57. map.put(sub, ps+ss);
  58. } else {
  59. map.put(sub, Math.max(map.get(sub), ps+ss));
  60. }
  61. }
  62. }
  63. int m = -1;
  64. Set<String> keys = map.keySet();
  65.  
  66. String[] arr = keys.toArray(new String[keys.size()]);
  67.  
  68. Arrays.sort(arr);
  69. String ans = null;
  70. for(int i = 0; i<arr.length; i++) {
  71. if(m < map.get(arr[i])) {
  72. ans = arr[i];
  73. m = map.get(arr[i]);
  74. }
  75. }
  76.  
  77. return ans;
  78. }
  79. }
Success #stdin #stdout 0.14s 57704KB
stdin
engine
raven
ginkgo
stdout
Text:
Prefix:
Suffix:
output:engin