public static void main(String[] args) {
   Scanner inp = new Scanner(System.in);
   int n = inp.nextInt();
   String[] store = new String[n];
   for (int i = 0; i < n; i++) store[i] = inp.next();
   int[] cnt = new int[n];
   Arrays.fill(cnt, 0);
   String str = inp.next();
   for (int j = 0; j < n; j++) {
     int l1 = store[j].length();
     for (int k = 0; k <= (str.length() - l1); k++) {
       if (str.substring(k, k + l1).equals(store[j])) {
         cnt[j] = cnt[j] + 1;
       }
     }
   }
   int y = 0;
   for (int m = 0; m < n; m++) {
     y = Math.max(y, cnt[m]);
   }
   System.out.println(y);
   for (int h = 0; h < n; h++) {
     if (cnt[h] == y) System.out.println(store[h]);
   }
 }
  public void init() {
    Scanner scan = new Scanner(System.in);
    n = scan.nextInt();
    m = scan.nextInt();
    for (int i = 0; i < n; i++) {
      String input = scan.next();
      record.add(input);
    }

    for (int i = 0; i < m; i++) {
      String input = scan.next();
      char[] charArray = input.toCharArray();
      boolean isValid = false;
      for (int j = 0; j < charArray.length; j++) {
        char c = charArray[j];
        for (char d = 'a'; d <= 'c'; d = (char) (d + 1)) {
          if (d == c) continue;
          charArray[j] = d;
          if (record.contains(new String(charArray))) {
            isValid = true;
            break;
          }
        }
        charArray[j] = c;
        if (isValid) break;
      }
      if (isValid) System.out.println("YES");
      else System.out.println("NO");
    }
    scan.close();
  }
Example #3
0
 public static void main(String[] args) {
   Scanner in = new Scanner(System.in);
   int n = in.nextInt();
   int[] data = new int[n];
   for (int i = 0; i < n; i++) data[i] = -1;
   for (int i = 0; i < n; i++) {
     in.next();
     if (in.nextInt() == 1) {
       data[i] = in.nextInt();
     }
   }
   int max = 10000;
   int max_sum = 0;
   int min_sum = 0;
   int cnt = 0;
   for (int i = 0; i < n; i++) {
     if (data[i] == -1) {
       max_sum += max;
       cnt++;
     } else {
       max = data[i];
       max_sum += data[i];
       min_sum += (cnt + 1) * data[i];
       cnt = 0;
     }
   }
   min_sum += cnt;
   // System.out.println(min_sum+" "+max_sum);
   if (min_sum <= 100 * 100 && max_sum >= 100 * 100) System.out.println("YES");
   else System.out.println("NO");
 }
Example #4
0
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println(3 ^ 5 ^ 4 ^ 5 ^ 5 ^ 6 ^ 6 ^ 7);
    System.out.println(3 ^ 7);

    int[] a = new int[10];
    for (int i = 0; i < 10; i++) a[i] = input.nextInt();
    for (int aa : a) {
      System.out.print(aa);
    }
    sss(1, 3, 4, 5, 6, 7, 8, 9);

    BigInteger aa =
        new BigInteger(
            "12809348038290834905895869048359034859083495894038590438590348590834905890345");
    BigInteger bb = new BigInteger("111111111111111111111");
    System.out.println(aa.subtract(bb));
    BigDecimal rrrr = new BigDecimal(input.next());
    int aaa;
    double b;
    float c;
    StringBuilder sssss = new StringBuilder("1234234");
    System.out.println();

    Integer ttt = 4;
    Double t1 = 14.2;
  }
Example #5
0
 public static void main(String[] args) {
   Scanner s = new Scanner(System.in);
   int n = s.nextInt();
   int m = s.nextInt();
   int[][] b = new int[n][m];
   for (int i = 0; i < n; i++) {
     String line = s.next();
     for (int j = 0; j < m; j++) {
       b[i][j] = line.charAt(j) - '0';
     }
   }
   int[][] a = new int[n][m];
   for (int i = 0; i < n - 2; i++) {
     for (int j = 1; j < m - 1; j++) {
       if (b[i][j] > 0) {
         a[i + 1][j] = b[i][j];
         b[i][j] -= a[i + 1][j];
         b[i + 1][j - 1] -= a[i + 1][j];
         b[i + 1][j + 1] -= a[i + 1][j];
         b[i + 2][j] -= a[i + 1][j];
       }
     }
   }
   for (int i = 0; i < n; i++) {
     for (int j = 0; j < m; j++) {
       System.out.print(a[i][j]);
     }
     System.out.println();
   }
 }
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int n = scanner.nextInt();
    System.out.println("You entered integer " + n);

    List<BigDecimal> BD = new ArrayList<BigDecimal>();
    // System.out.println("Enter a string");
    // n = scanner.nextInt();
    /*  while(scanner.hasNext()){
    	  BD.add(new BigDecimal(scanner.next()));
    	  break;
    }

      for(BigDecimal bd :BD){
    	  System.out.println(bd);
    	  }  */

    for (int i = 0; i < n; i++) {
      BD.add(new BigDecimal(scanner.next()));
    }

    // Collections.reverse(BD);
    Collections.sort(BD, Collections.reverseOrder());
    for (BigDecimal bd : BD) {
      System.out.println(bd);
    }
  }
Example #7
0
 void solve(int caseNum) {
   chars = in.next().toCharArray();
   Node tree = new Node();
   tree.stretch();
   System.out.println(caseNum);
   System.out.print(tree);
 }
Example #8
0
 public static void main(String[] args) {
   String z = in.next();
   int k = in.nextInt();
   int s = in.nextInt() - 1;
   int l = in.nextInt();
   int[] t = new int[l];
   for (int i = 0; i < l; ++i) {
     t[i] = in.nextInt() - 1;
   }
   int[][] f = new int[z.length()][k];
   for (int i = 0; i < k; ++i) {
     for (int j = 0; j < z.length(); ++j) {
       f[j][i] = in.nextInt() - 1;
     }
   }
   boolean[][] g = new boolean[z.length()][k];
   for (int i = 0; i < k; ++i) {
     for (int j = 0; j < z.length(); ++j) {
       g[j][i] = (in.nextInt() == 0);
     }
   }
   DFA dfa = new DFA(z.toCharArray(), k, s, t, f, g);
   int n = in.nextInt();
   out.println(dfa.count(n));
   out.flush();
 }
Example #9
0
  boolean solve(int caseNum) {
    if (!in.hasNextInt()) return false;
    int n = in.nextInt();
    if (n == 0) return false;

    UFNode[] graph = new UFNode[n + 1];
    int[] yes = new int[2];
    for (int i = 0; i < graph.length; i++) graph[i] = new UFNode();

    PARSE:
    while (true) {
      switch (in.next().charAt(0)) {
        case 'c':
          graph[in.nextInt()].merge(graph[in.nextInt()]);
          break;
        case 'd':
          graph[in.nextInt()].remove();
          break;
        case 'q':
          yes[graph[in.nextInt()].find() == graph[in.nextInt()].find() ? 0 : 1]++;
          break;
        case 'e':
          break PARSE;
      }
    }

    System.out.println(yes[0] + " , " + yes[1]);

    return true;
  }
Example #10
0
 public static void main(String[] args) {
   Scanner in = new Scanner(System.in);
   n = in.nextInt();
   l = new BigInteger(in.next());
   k = in.nextInt();
   m = in.nextInt();
   BigInteger w = BigInteger.TEN.pow(m);
   for (int i = 0; i <= n; ++i) a[i] = new BigInteger(in.next());
   for (int i = 0; i < Math.min(k, n + 1); ++i) {
     t = a[0];
     for (int j = 1; j <= n; ++j) {
       t = t.multiply(l);
       t = t.add(a[j]);
     }
     t = t.mod(w);
     q[n][i] = t.mod(w);
     int ret = 0;
     c = t.toString().toCharArray();
     int ll = c.length;
     for (int j = 0; j < Math.min(m, ll); ++j) {
       ret += (c[ll - 1 - j] - '0') * (c[ll - 1 - j] - '0');
     }
     System.out.println(ret);
     l = l.add(BigInteger.ONE);
   }
   if (k > n) {
     for (int i = n - 1; i >= 0; --i) {
       for (int j = 0; j <= i; j++) q[i][j] = q[i + 1][j + 1].subtract(q[i + 1][j]).mod(w);
     }
     for (int i = 1; i <= n; ++i) q[0][i] = q[0][0];
     int po = 1;
     for (int i = n + 1; i < k; ++i) {
       for (int j = 1; j <= n; ++j)
         q[j][(po + j) % (n + 1)] =
             q[j - 1][(po + j - 1) % (n + 1)].add(q[j][(po + j - 1) % (n + 1)]).mod(w);
       int ret = 0;
       c = q[n][(po + n) % (n + 1)].mod(w).toString().toCharArray();
       int ll = c.length;
       for (int j = 0; j < Math.min(m, ll); ++j) {
         ret += (c[ll - 1 - j] - '0') * (c[ll - 1 - j] - '0');
       }
       System.out.println(ret);
       l = l.add(BigInteger.ONE);
       ++po;
     }
   }
 }
Example #11
0
 public static void main(String[] args) {
   Scanner s = new Scanner(System.in);
   BigInteger a = new BigInteger(s.next());
   BigInteger b = new BigInteger(s.next());
   BigInteger ad = a.add(b);
   BigInteger mu = a.multiply(b);
   System.out.println(ad);
   System.out.println(mu);
 }
Example #12
0
  public static void main(String[] args) throws IOException {
    Scanner scanner = new Scanner(new BufferedInputStream(System.in));

    int num[][] = {
      {0, 2, 3, 5, 6, 7, 8, 9},
      {2, 3, 4, 5, 6, 8, 9},
      {0, 2, 3, 5, 6, 8, 9},
      {0, 4, 5, 6, 8, 9},
      {0, 1, 2, 3, 4, 7, 8, 9},
      {0, 2, 6, 8},
      {0, 1, 3, 4, 5, 6, 7, 8, 9}
    };

    while (scanner.hasNextInt()) {
      int a = scanner.nextInt();
      String b = scanner.next();
      if (a == 0) break;
      String data[][] = new String[5 + (a - 1) * 2][(3 + (a - 1)) * b.length()];

      for (int i = 0; i < 5 + (a - 1) * 2; i++)
        for (int j = 0; j < (3 + (a - 1)) * b.length(); j++) data[i][j] = " ";

      for (int i = 0; i < b.length(); i++) {
        for (int j = 0; j < 3; j++) {
          for (int k = 0; k < num[j].length; k++) {
            if ((int) b.charAt(i) - 48 == num[j][k]) {
              for (int h = 0; h < a; h++) data[(a + 1) * j][1 + i * (a + 2) + h] = "-";
            }
          }
        }
        for (int j = 3; j < 5; j++) {
          for (int k = 0; k < num[j].length; k++) {
            if ((int) b.charAt(i) - 48 == num[j][k]) {
              for (int h = 0; h < a; h++) data[1 + h][(j - 3) * (a + 1) + (i * (a + 2))] = "|";
            }
          }
        }
        for (int j = 5; j < 7; j++) {
          for (int k = 0; k < num[j].length; k++) {
            if ((int) b.charAt(i) - 48 == num[j][k]) {
              for (int h = 0; h < a; h++) data[a + 2 + h][(j - 5) * (a + 1) + (i * (a + 2))] = "|";
            }
          }
        }
      }
      StringBuffer sb = new StringBuffer("");
      for (int i = 0; i < 5 + (a - 1) * 2; i++) {
        for (int j = 0; j < (3 + (a - 1)) * b.length(); j++) {
          if ((j - (a + 1)) % (a + 2) == 0 && (j + 1) != (3 + (a - 1)) * b.length())
            sb.append(data[i][j] + " ");
          else sb.append(data[i][j]);
        }
        sb.append("\n");
      }
      System.out.println(sb);
    }
  }
Example #13
0
 public void loadWords(Scanner S) {
   for (int i = 0; i < words.length; i++) {
     S.nextLine();
     // makes the words upper case so that guesses
     // are case insensitive
     words[i] = S.next().toUpperCase();
     played[i] = new String("");
   }
 }
Example #14
0
 public static void main(String[] args) {
   Scanner in = new Scanner(System.in);
   int m;
   m = in.nextInt();
   String grid[] = new String[m];
   for (int i = 0; i < m; i++) {
     grid[i] = in.next();
   }
   displayPathtoPrincess(m, grid);
 }
Example #15
0
  public static void main(String[] args) throws IOException {
    Scanner scanner = new Scanner(new BufferedInputStream(System.in));

    while (scanner.hasNext()) {
      String str = scanner.next();
      if (str.equals("*")) break;
      if ((int) str.charAt(0) < 97) num(str);
      else abcd(str);
    }
  }
 public static void main(String[] args) {
   Scanner in = new Scanner(System.in);
   int t = in.nextInt();
   for (int a0 = 0; a0 < t; a0++) {
     int R = in.nextInt();
     int C = in.nextInt();
     String G[] = new String[R];
     for (int G_i = 0; G_i < R; G_i++) {
       G[G_i] = in.next();
     }
     int r = in.nextInt();
     int c = in.nextInt();
     String P[] = new String[r];
     for (int P_i = 0; P_i < r; P_i++) {
       P[P_i] = in.next();
     }
     // Check Grid
     checkGrid(G, P);
   }
 }
Example #17
0
 static void solve() {
   String[] s = in.next().split(",");
   Point p =
       reflect(
           new Line(
               Double.parseDouble(s[0]),
               Double.parseDouble(s[1]),
               Double.parseDouble(s[2]),
               Double.parseDouble(s[3])),
           new Point(Double.parseDouble(s[4]), Double.parseDouble(s[5])));
   out.println(p.getX() + " " + p.getY());
 }
Example #18
0
 void solve(int caseNum) {
   int n = in.nextInt();
   BigInteger[] past = new BigInteger[n], d = new BigInteger[n];
   for (int i = 0; i < n; i++) past[i] = new BigInteger(in.next());
   Arrays.sort(past);
   for (int i = 0; i < n; i++) d[i] = past[i].subtract(past[0]);
   for (int i = 2; i < n; i++) d[i] = d[i].gcd(d[i - 1]);
   BigInteger gcd = d[n - 1], mod = past[0].mod(gcd);
   System.out.printf("Case #%d: ", caseNum);
   if (mod.equals(BigInteger.ZERO)) System.out.println(0);
   else System.out.println(gcd.subtract(mod));
 }
Example #19
0
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    String line = System.getProperty("line.separator");
    scan.useDelimiter(line);

    int cases = scan.nextInt();

    for (int x = 0; x < cases; x++) {

      String[] get = scan.next().split(" ");

      System.out.println(Integer.parseInt(get[0]) % Integer.parseInt(get[1]));
    }
  }
Example #20
0
  public static void main(String[] args) throws IOException {

    Scanner scanner = new Scanner(new BufferedInputStream(System.in));
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    while (scanner.hasNext()) {
      String str = scanner.next();
      if (str.equals("0")) break;
      Long sum = 0L;

      for (int i = str.length() - 1, j = 1; i >= 0; i--, j++) {
        sum = sum + ((long) Math.pow(2, j) - 1) * ((long) str.charAt(i) - 48);
      }
      System.out.println(sum);
    }
  }
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String time = in.next();

    // Set format for input and output
    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ssa");
    SimpleDateFormat out = new SimpleDateFormat("HH:mm:ss");

    try {
      Date inTime = sdf.parse(time);
      System.out.println(out.format(inTime));
    } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  public static void main(String args[]) {
    Scanner in = new Scanner(System.in);
    int N = in.nextInt();
    Node head = new Node();

    for (int i = 0; i < N; i++) {
      String telephone = in.next();
      put(head, telephone);
    }

    // Write an action using System.out.println()
    // To debug: System.err.println("Debug messages...");

    System.out.println(
        countNoOfElememts(
            head)); // The number of elements (referencing a number) stored in the structure.
  }
Example #23
0
  public static void main(String args[]) {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    for (int i = 0; i < n; i++) {
      String t = in.next();

      if (Integer.valueOf(t.substring(0, 1))
              + Integer.valueOf(t.substring(1, 2))
              + Integer.valueOf(t.substring(2, 3))
          == Integer.valueOf(t.substring(3, 4))
              + Integer.valueOf(t.substring(4, 5))
              + Integer.valueOf(t.substring(5, 6))) {
        System.out.println("true");
      } else {
        System.out.println("false");
      }
    }
  }
Example #24
0
  public static void main(String[] args) throws IOException {

    Scanner scanner = new Scanner(new BufferedInputStream(System.in));
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    int num;
    while ((num = scanner.nextInt()) != 0) {
      ArrayList<String> all = new ArrayList<String>();
      for (int i = 0; i < num; i++) {
        all.add(scanner.next());
      }
      Collections.sort(all, comptr);
      StringBuilder sb = new StringBuilder("");
      for (String s : all) {
        sb.append(s);
      }
      System.out.println(sb);
    }
  }
Example #25
0
    protected String solve(Scanner sc) {
      int N = sc.nextInt();
      int K = sc.nextInt();
      char[][] board = new char[N][];
      for (int i = 0; i < N; i++) board[i] = sc.next().toCharArray();
      char[][] rot = new char[N][N];
      for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) rot[i][j] = board[N - j - 1][i];
      char[][] grav = new char[N][N];
      for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) grav[i][j] = '.';
      for (int i = 0; i < N; i++)
        for (int j = N - 1, c = 0; j >= 0; j--)
          if (rot[j][i] == '.') c++;
          else grav[j + c][i] = rot[j][i];
      //			for (char[] cs : grav)
      //				debug(new String(cs));
      int[][][] R = new int[N][N][4];
      int[][][] B = new int[N][N][4];
      for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
          switch (grav[i][j]) {
            case 'B':
              {
                for (int d = 0; d < 4; d++)
                  B[i][j][d] = (in(i + di[d], j + dj[d], N) ? B[i + di[d]][j + dj[d]][d] : 0) + 1;
                break;
              }
            case 'R':
              {
                for (int d = 0; d < 4; d++)
                  R[i][j][d] = (in(i + di[d], j + dj[d], N) ? R[i + di[d]][j + dj[d]][d] : 0) + 1;
                break;
              }
          }
      boolean Rwin = false, Bwin = false;
      for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
          for (int d = 0; d < 4; d++) {
            if (R[i][j][d] >= K) Rwin = true;
            if (B[i][j][d] >= K) Bwin = true;
          }

      return Rwin ? Bwin ? "Both" : "Red" : Bwin ? "Blue" : "Neither";
    }
Example #26
0
File: A.java Project: t8m8/AtCoder
  static void solve() {
    int r = in.nextInt();
    int c = in.nextInt();
    int sy = in.nextInt() - 1;
    int sx = in.nextInt() - 1;
    int gy = in.nextInt() - 1;
    int gx = in.nextInt() - 1;
    char[][] t = new char[r][c];
    for (int i = 0; i < r; i++) {
      t[i] = in.next().toCharArray();
    }

    ArrayDeque<int[]> que = new ArrayDeque<>();
    BitSet visited = new BitSet();

    que.add(new int[] {sy, sx, 0});
    visited.set(sy * c + sx);

    int[] dx = {0, 1, 0, -1};
    int[] dy = {1, 0, -1, 0};
    int[][] dist = new int[r][c];

    while (!que.isEmpty()) {
      int[] p = que.pollFirst();
      int cy = p[0];
      int cx = p[1];
      int d = p[2];
      dist[cy][cx] = d;

      for (int i = 0; i < 4; i++) {
        int ny = cy + dy[i];
        int nx = cx + dx[i];
        if (ny < 0 || nx < 0 || r <= ny || c <= nx) continue;
        if (visited.get(ny * c + nx) || t[ny][nx] == '#') continue;
        que.add(new int[] {ny, nx, d + 1});
        visited.set(ny * c + nx);
      }
    }

    out.println(dist[gy][gx]);
  }
Example #27
0
 public static void main(String[] args) throws NumberFormatException, IOException {
   Scanner sc = new Scanner(System.in);
   int N = sc.nextInt();
   int M = sc.nextInt();
   int maxValue = (int) Math.pow(2, 5) - 1;
   int[] array = new int[N];
   int first = 0;
   int second = 0;
   for (int i = 0; i < N; i++) {
     array[i] = Integer.parseInt(sc.next(), 2);
   }
   for (int i = 0; i < array.length; i++) {
     for (int j = i + 1; j < array.length; j++) {
       int or = (array[i] | array[j]);
       if (or > first) {
         first = or;
       }
     }
   }
   System.out.println(first + " " + second);
 }
Example #28
0
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        T = sc.nextInt();
        for (int _ = 0; _ < T; _++) {
            n = sc.nextInt();
            a = sc.next();
            scs = new BigInteger[n];
            for (int i = 0; i < n; i++) {
                scs[i] = BigInteger.ZERO;
            }
            int ix = 0, cur = 0;
            while (ix < a.length()) {
                ix = turn(cur, ix);
                cur = (cur + 1) % n;
            }
//            System.out.println(n + ": " + a);
//            for (int i = 0; i < n; i++) System.out.println(scs[i]);
            Arrays.sort(scs);
            System.out.println(scs[n-1]);
        }
    }
Example #29
0
  public static void main(String[] args) {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    Scanner sc = new Scanner(System.in);
    String s1 = sc.next();
    String s2 = sc.next();

    int[][] f = new int[s1.length()][s2.length()];

    for (int i = 0; i < s1.length(); i++) {
      for (int j = 0; j < s2.length(); j++) {
        f[i][j] = 0;
      }
    }

    for (int i = 0; i < s1.length(); i++) {
      for (int j = 0; j < s2.length(); j++) {
        // important!
        if (i == 0 || j == 0) {
          if (s1.charAt(i) == s2.charAt(j)) {
            f[i][j] = 1;
          } else {
            if (i > 0) {
              f[i][j] = f[i - 1][j];
            }
            if (j > 0) {
              f[i][j] = f[i][j - 1];
            }
          }
        } else {
          if (s1.charAt(i) == s2.charAt(j)) {
            f[i][j] = getMax(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1] + 1);
          } else {
            f[i][j] = getMax(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]);
          }
        }
      }
    }

    System.out.println(f[s1.length() - 1][s2.length() - 1]);
  }
Example #30
0
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String s = in.next();

    int l = s.length();
    Double sqrl = Math.sqrt(l);
    Double low = Math.floor(sqrl);
    Double high = Math.ceil(sqrl);
    int r = 0;
    int c = 0;

    if (low == high) {
      r = low.intValue();
      c = low.intValue();
    } else if (low * high >= l) {
      r = low.intValue();
      c = high.intValue();
    } else {
      r = high.intValue();
      c = high.intValue();
    }

    //        System.out.println("Row: " + r);
    //        System.out.println("Col: " + c);

    StringBuilder sb = new StringBuilder();
    char en[] = s.toCharArray();
    for (int i = 0; i < c; i++) {
      for (int j = 0; j < r; j++) {
        if (j * c + i < l) {
          sb.append(en[j * c + i]);
        }
      }
      sb.append(" ");
    }

    System.out.println(sb.toString());
  }