fork(1) download
  1. import java.io.*;
  2. import java.util.*;
  3. public class Main {
  4. public static void main(String[] args) {
  5. InputStream inputStream = System.in;
  6. OutputStream outputStream = System.out;
  7. InputReader in = new InputReader(inputStream);
  8. OutputWriter out = new OutputWriter(outputStream);
  9. TaskB solver = new TaskB();
  10. solver.solve(in, out);
  11. out.close();
  12. }
  13. }
  14. class TaskB {
  15.  
  16. public void solve(InputReader in, OutputWriter out) {
  17.  
  18. System.out.println(calculateSum("GAAAGGCCCCCCGGG", "GAAAGGCCCCCCGGG")); // 1215
  19. System.out.println(calculateSum("GAAAGGCCCCCCGGG","GTTTGGCCCCCCGGG")); // 1080
  20. }
  21.  
  22. private long calculateSum(String s, String t) {
  23. long sum = 0;
  24. for(int i=0;i<s.length();i++) {
  25. for(int j=0;j<t.length();j++) {
  26. sum += calculate(s,t);
  27. t = (t.substring(1,t.length()) + t.charAt(0));
  28. }
  29. s = (s.substring(1,s.length()) + s.charAt(0));
  30. }
  31. return sum;
  32. }
  33.  
  34. private long calculate(String s, String t) {
  35. long sum = 0;
  36. for(int i=0;i<s.length();i++) {
  37. if(s.charAt(i) == t.charAt(i)) sum++;
  38. }
  39. return sum;
  40. }
  41.  
  42. }
  43.  
  44.  
  45. class InputReader {
  46. public BufferedReader reader;
  47. public StringTokenizer tokenizer;
  48. public InputReader(InputStream stream) {
  49. reader = new BufferedReader(new InputStreamReader(stream), 32768);
  50. tokenizer = null;
  51. }
  52. public String next() {
  53. while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  54. try {
  55. tokenizer = new StringTokenizer(reader.readLine());
  56. } catch (IOException e) {
  57. throw new RuntimeException(e);
  58. }
  59. }
  60. return tokenizer.nextToken();
  61. }
  62. public boolean hasNext() {
  63. while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  64. try {
  65. String line = reader.readLine();
  66. if ( line == null ) {
  67. return false;
  68. }
  69. tokenizer = new StringTokenizer(line);
  70. } catch (IOException e) {
  71. throw new RuntimeException(e);
  72. }
  73. }
  74. return true;
  75. }
  76. public int nextInt() {
  77. return Integer.parseInt(next());
  78. }
  79. public long nextLong() {
  80. return Long.parseLong(next());
  81. }
  82. public double nextDouble() { return Double.parseDouble(next());}
  83. }
  84. class OutputWriter {
  85. private final PrintWriter writer;
  86. public OutputWriter(OutputStream outputStream) {
  87. writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
  88. }
  89. public OutputWriter(Writer writer) {
  90. this.writer = new PrintWriter(writer);
  91. }
  92. public void print(Object... objects) {
  93. for (int i = 0; i < objects.length; i++) {
  94. if ( i != 0 ) {
  95. writer.print(' ');
  96. }
  97. writer.print(objects[i]);
  98. }
  99. }
  100. public void printLine(Object... objects) {
  101. print(objects);
  102. writer.println();
  103. }
  104. public void close() {
  105. writer.close();
  106. }
  107. }
Success #stdin #stdout 0.11s 320256KB
stdin
Standard input is empty
stdout
1215
1080