Jovfer

taskG 03112013 GP of Siberia

Nov 3rd, 2013
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1.     int n;
  2.     String[] dict;
  3.     boolean[][] isChecked;
  4.  
  5.     private int getPrefixLength(String a, int posA, String b, int posB) {
  6.         int result = 0;
  7.         while (result + posA < a.length() && result + posB < b.length()
  8.         && a.charAt(result + posA) == b.charAt(result + posB)) {
  9.             result++;
  10.         }
  11.         return result;
  12.     }
  13.  
  14.     private boolean slv(String str, int pos, int ind) {
  15.         boolean result = false;
  16.         if (isChecked[ind][pos]) {
  17.             return false;
  18.         }
  19.         isChecked[ind][pos] = true;
  20.         int commonPrefix;
  21.         for (int i = 0; i < dict.length && !result; i++) {
  22.             commonPrefix = getPrefixLength(str, pos, dict[i], 0);
  23.             if (commonPrefix != Math.min(str.length() - pos, dict[i].length())) {
  24.                 continue;
  25.             }
  26.             if (pos + commonPrefix == str.length() && commonPrefix == dict[i].length()) {
  27.                 return true;
  28.             }
  29.             if (pos + commonPrefix == str.length()) {
  30.                 if (!isChecked[i][commonPrefix]) {
  31.                     result |= slv(dict[i], commonPrefix, i);
  32.                 }
  33.             } else {
  34.                 if (!isChecked[ind][pos + commonPrefix]) {
  35.                     result |= slv(str, pos + commonPrefix, ind);
  36.                 }
  37.             }
  38.         }
  39.         return result;
  40.     }
  41.  
  42.     public void solve() throws NumberFormatException, IOException {
  43.         n = nextInt();
  44.         dict = new String[n];
  45.         isChecked = new boolean[n][];
  46.         for (int i = 0; i < dict.length; i++) {
  47.             dict[i] = in.readLine();
  48.             isChecked[i] = new boolean[dict[i].length()];
  49.             for (int j = 0; j < dict[i].length(); j++) {
  50.                 isChecked[i][j] = false;
  51.             }
  52.         }
  53.         for (int i = 0; i < n - 1; i++) {
  54.             for (int j = i + 1; j < n; j++) {
  55.                 int ind = i;
  56.                 String a = dict[i];
  57.                 String b = dict[j];
  58.                 int com = getPrefixLength(a, 0, b, 0);
  59.                 if (com != Math.min(a.length(), b.length())) {
  60.                     continue;
  61.                 }
  62.                 if (b.length() > a.length()) {
  63.                     a = b;
  64.                     ind = j;
  65.                 }
  66.                 if (isChecked[ind][com]) {
  67.                     continue;
  68.                 }
  69.                 if (slv(a, com, ind)) {
  70.                     out.println("YES");
  71.                     return;
  72.                 }
  73.             }
  74.         }
  75.         out.println("NO");
  76.     }
Advertisement
Add Comment
Please, Sign In to add comment