void generate(int[] p, int L, int R) {
        if (L == R) { // all numbers are set
            
            boolean[] b = new boolean[strCnt];
            
            for (int i = 0; i < strCnt - 1; i++) {
                String before = strArray[p[i]];
                String next = strArray[p[i + 1]];

                if (before.charAt(before.length() - 1) == next.charAt(0)) {
                    b[p[i]] = true;
                    b[p[i + 1]] = true;
                }
            }
            for (boolean c : b) {
                if (!c) {
                    return;
                }
            }
            
            sum ++;
            
//            System.out.println(deepToString(convIntArray(p)));
            //            System.out.print("OK :");

            for (int i = 0; i < strCnt; i++) {
//                System.out.print(strArray[p[i]] + ",");
            }
//            System.out.println();
        } else { // numbers at positions [0, L-1] are set, try to set L-th position
          for (int i = L; i <= R; i++) {
            int tmp = p[L]; p[L] = p[i]; p[i] = tmp;
            generate(p, L+1, R);
            tmp = p[L]; p[L] = p[i]; p[i] = tmp;
          }
        }
     }