Advertisement
Guest User

Untitled

a guest
Feb 5th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main {
  4. public static int lcp(String str1, String str2) {
  5. int count = 0;
  6. for (int i = 0; i < Math.min(str1.length(), str2.length()); i++) {
  7. if (str1.charAt(i) == str2.charAt(i)) count++;
  8. else break;
  9. }
  10. return count;
  11. }
  12.  
  13. public static void main (String[] args) {
  14. Scanner s = new Scanner(System.in);
  15. String[] sa;
  16. int n = s.nextInt();
  17. String str;
  18. int count;
  19. for (int i = 0; i < n; i++) {
  20. str = s.next();
  21. sa = new String[str.length()];
  22. for (int j = 0; j < str.length(); j++) {
  23. sa[j] = str.substring(j);
  24. }
  25. Arrays.sort(sa);
  26. count = 0;
  27. count += sa[0].length() + 1;
  28. for (int j = 0; j < str.length() - 1; j++) {
  29. count += sa[j+1].length() - lcp(sa[j], sa[j+1]);
  30. }
  31. System.out.println(count);
  32. }
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement