fork download
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Arrays;
  5.  
  6.  
  7. public class Main {
  8.  
  9. public static void main(String[] args) throws IOException {
  10. int t = Integer.parseInt(bf.readLine());
  11. while(t-->0){
  12. String s = bf.readLine();
  13. int n = s.length();
  14. suffixsarray x = new suffixsarray(s);
  15. int res = n - x.index(0);
  16. for(int i=1; i<n; i++)
  17. res += (n-x.index(i)) - x.lcp(i);
  18. System.out.println(res);
  19. }
  20. }
  21. }
  22.  
  23. class suffixsarray {
  24. private Suffix[] suffixes;
  25.  
  26. public suffixsarray(String text) {
  27. int N = text.length();
  28. this.suffixes = new Suffix[N];
  29. for (int i = 0; i < N; i++)
  30. suffixes[i] = new Suffix(text, i);
  31. Arrays.sort(suffixes);
  32. }
  33.  
  34. private static class Suffix implements Comparable<Suffix> {
  35. private final String text;
  36. private final int index;
  37.  
  38. private Suffix(String text, int index) {
  39. this.text = text;
  40. this.index = index;
  41. }
  42.  
  43. private int length() {
  44. return text.length() - index;
  45. }
  46.  
  47. private char charAt(int i) {
  48. return text.charAt(index + i);
  49. }
  50.  
  51. public int compareTo(Suffix that) {
  52. if (this == that)
  53. return 0; // optimization
  54. int N = Math.min(this.length(), that.length());
  55. for (int i = 0; i < N; i++) {
  56. if (this.charAt(i) < that.charAt(i))
  57. return -1;
  58. if (this.charAt(i) > that.charAt(i))
  59. return +1;
  60. }
  61. return this.length() - that.length();
  62. }
  63.  
  64. public String toString() {
  65. return text.substring(index);
  66. }
  67. }
  68.  
  69. // length of string
  70. public int length() {
  71. return suffixes.length;
  72. }
  73.  
  74. // index of ith sorted suffix
  75. public int index(int i) {
  76. return suffixes[i].index;
  77. }
  78.  
  79. // longest common prefix of suffixes(i) and suffixes(i-1)
  80. public int lcp(int i) {
  81. return lcp(suffixes[i], suffixes[i - 1]);
  82. }
  83.  
  84. // longest common prefix of s and t
  85. private static int lcp(Suffix s, Suffix t) {
  86. int N = Math.min(s.length(), t.length());
  87. for (int i = 0; i < N; i++) {
  88. if (s.charAt(i) != t.charAt(i))
  89. return i;
  90. }
  91. return N;
  92. }
  93.  
  94. public String select(int i) {
  95. return suffixes[i].toString();
  96. }
  97.  
  98. // number of suffixes strictly less than query
  99. public int rank(String query) {
  100. int lo = 0, hi = suffixes.length - 1;
  101. while (lo <= hi) {
  102. int mid = lo + (hi - lo) / 2;
  103. int cmp = compare(query, suffixes[mid]);
  104. if (cmp < 0)
  105. hi = mid - 1;
  106. else if (cmp > 0)
  107. lo = mid + 1;
  108. else
  109. return mid;
  110. }
  111. return lo;
  112. }
  113.  
  114. // compare query string to suffix
  115. private static int compare(String query, Suffix suffix) {
  116. int N = Math.min(query.length(), suffix.length());
  117. for (int i = 0; i < N; i++) {
  118. if (query.charAt(i) < suffix.charAt(i))
  119. return -1;
  120. if (query.charAt(i) > suffix.charAt(i))
  121. return +1;
  122. }
  123. return query.length() - suffix.length();
  124. }
  125. }
Runtime error #stdin #stdout #stderr 0.07s 380160KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.NumberFormatException: null
	at java.lang.Integer.parseInt(Integer.java:454)
	at java.lang.Integer.parseInt(Integer.java:527)
	at Main.main(Main.java:11)