POJ 4092:牛仔裤

不知道为什么叫牛仔裤。。

  1 import java.util.Scanner;
  2 
  3 public class Main {
  4 
  5     public static void main(String[] args) {
  6         Scanner in = new Scanner(System.in);
  7         int n = in.nextInt();
  8         for (int i = 0; i < n; i++) {
  9             int m = in.nextInt();
 10             String s = in.nextLine();
 11             String[] strings = new String[m];
 12             for (int j = 0; j < m; j++) {
 13                 strings[j] = in.nextLine();
 14             }
 15             
 16             printSub(getPublicSubString(strings));
 17         }
 18     }
 19     
 20     /**
 21      * 获得公共的最大子串
 22      * @param strings
 23      * @return
 24      */
 25     private static String getPublicSubString(String[] strings) {
 26         String[] subs = getSubString(strings[0]); // 这里只对第0个输入字符串进行获取子串
 27         
 28         int maxLength = 0; // 记录子串的最大长度
 29         int maxIndex = 0; // 记录最长子串的下标
 30         for (int j = 0; j < subs.length; j++) {
 31             boolean isAll = true; // 判断当前子串是否是所有字符串的子串
 32             for (int k = 1; k < strings.length; k++) {
 33                 if (!isSubString(subs[j], strings[k])) {
 34                     isAll = false;
 35                     break;
 36                 }
 37             }
 38             
 39             // 如果当前子串是所有字符串的子串,并且子串长度比之前得到的子串最长的还要长,重新赋值
 40             if (isAll && subs[j].length() > maxLength) {
 41                 maxLength = subs[j].length();
 42                 maxIndex = j;
 43             }
 44         }
 45         
 46         return subs[maxIndex];
 47     }
 48     
 49     /**
 50      * 打印子串
 51      * @param sub
 52      */
 53     private static void printSub(String sub) {
 54         if (sub.length() < 3) {
 55             System.out.println("no significant commonalities");
 56         } else {
 57             System.out.println(sub);
 58         }
 59     }
 60     
 61     /**
 62      * 获得一个字符串的所有子串
 63      * @param s
 64      * @return
 65      */
 66     private static String[] getSubString(String s) {
 67         int count = getSubStringCount(s);
 68         String[] subs = new String[count];
 69         // TODO
 70         // i为开始位置,j为结束位置
 71         for (int i = 0, ct = 0; i < s.length(); i++) {
 72             for (int j = i + 1; j <= s.length(); j++) {
 73                 char[] sub = new char[j - i];
 74                 for (int k = i; k < j; k++) {
 75                     sub[k - i] = s.charAt(k);
 76                 }
 77                 subs[ct++] = String.valueOf(sub);
 78             }
 79         }
 80         
 81         return subs;
 82     }
 83     
 84     /**
 85      * 获得一个字符串中共有多少个子串
 86      * @param s
 87      * @return
 88      */
 89     private static int getSubStringCount(String s) {
 90         return (s.length() * (s.length() + 1)) / 2;
 91     }
 92     
 93     /**
 94      * 判断串s1是否是串s2的子串
 95      * @param s1
 96      * @param s2
 97      * @return
 98      */
 99     private static boolean isSubString(String s1, String s2) {
100         if (s1.length() > s2.length()) {
101             return false;
102         }
103         boolean isSub = false;
104         for (int i = 0; i <= s2.length() - s1.length(); i++) {
105             int j;
106             for (j = 0; j < s1.length(); j++) {
107                 if (s1.charAt(j) != s2.charAt(i + j)) {
108                     break;
109                 }
110             }
111             if (j == s1.length()) {
112                 isSub = true;
113                 break;
114             }
115         }
116         return isSub;
117     }
118 
119 }

 

你可能感兴趣的:(poj)