Exemplo n.º 1
1
 public BigInteger count(int n) {
   BigInteger[] prev = new BigInteger[k];
   Arrays.fill(prev, BigInteger.ZERO);
   prev[s] = BigInteger.ONE;
   while (n-- > 0) {
     BigInteger[] next = new BigInteger[k];
     Arrays.fill(next, BigInteger.ZERO);
     for (int i = 0; i < k; ++i) {
       if (prev[i].signum() <= 0) {
         continue;
       }
       for (int j = 0; j < z; ++j) {
         if (e[j][i] < 0) {
           continue;
         }
         next[e[j][i]] = next[e[j][i]].add(prev[i]);
       }
     }
     prev = next;
   }
   BigInteger ans = BigInteger.ZERO;
   for (int term : t) {
     ans = ans.add(prev[term]);
   }
   return ans;
 }
Exemplo n.º 2
1
 public int minimumPrice(long[] dread, int[] price) {
   int n = dread.length;
   int m = n << 1;
   long[] maximum = new long[m + 1];
   Arrays.fill(maximum, 0);
   for (int i = 0; i < n; ++i) {
     long[] new_maximum = new long[m + 1];
     Arrays.fill(new_maximum, -1);
     for (int j = 0; j <= m; ++j) {
       if (maximum[j] != -1) {
         if (maximum[j] >= dread[i]) {
           new_maximum[j] = Math.max(new_maximum[j], maximum[j]);
         }
         if (j + price[i] <= m) {
           new_maximum[j + price[i]] = Math.max(new_maximum[j + price[i]], maximum[j] + dread[i]);
         }
       }
     }
     maximum = new_maximum;
   }
   int answer = 0;
   while (maximum[answer] == -1) {
     answer++;
   }
   return answer;
 }
Exemplo n.º 3
0
 public long numWays(int width, int height, String[] bad) {
   dpTable = new long[width][height];
   final boolean valid[][] = new boolean[width][height];
   for (int i = 0; i < width; ++i) {
     Arrays.fill(dpTable[i], -1l);
     Arrays.fill(valid[i], true);
   }
   dpTable[0][0] = 0l;
   for (int i = 0; i < width; ++i) {
     for (int j = 0; j < height; ++j) {}
   }
   return 0l;
 }
Exemplo n.º 4
0
 public void run() {
   try {
     int n = reader.nextInt();
     int[] a = new int[n];
     for (int i = 0; i < n; ++i) {
       a[i] = reader.nextInt();
     }
     BigInteger[] ways = new BigInteger[4];
     ways[0] = ways[1] = BigInteger.ONE;
     ways[2] = ways[3] = BigInteger.ZERO;
     for (int i = 0; i < n; ++i) {
       BigInteger[] newWays = new BigInteger[4];
       Arrays.fill(newWays, BigInteger.ZERO);
       for (int mask = 0; mask < 4; ++mask) {
         for (int now = 0; now < 2; ++now) {
           int newMask = mask << 1 | now;
           if (Integer.bitCount(newMask) == a[i]) {
             newWays[newMask & 3] = newWays[newMask & 3].add(ways[mask]);
           }
         }
       }
       ways = newWays;
     }
     BigInteger answer = ways[0].add(ways[2]);
     writer.println(answer);
   } catch (IOException ex) {
   }
   writer.close();
 }
 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]);
   }
 }
Exemplo n.º 6
0
  void run() {
    InputReader in = new InputReader(System.in);
    PrintWriter out = new PrintWriter(System.out);

    int n = in.nextInt(), q = in.nextInt();
    int[] v = new int[n], c = new int[n];
    for (int i = 0; i < n; ++i) v[i] = in.nextInt();
    for (int i = 0; i < n; ++i) c[i] = in.nextInt();

    for (int i = 0; i < q; ++i) {
      int a = in.nextInt(), b = in.nextInt();
      long[] dp = new long[n + 1];
      Node[] heap = new Node[2];
      Arrays.fill(dp, -INF);
      for (int j = 0; j < 2; ++j) heap[j] = new Node(-INF, -1);
      for (int j = 0; j < n; ++j) {
        long val = v[j], maxv = val * b;
        int color = c[j];
        maxv = Math.max(dp[color] + val * a, maxv);
        maxv = Math.max(choose(heap, color) + val * b, maxv);
        dp[color] = Math.max(maxv, dp[color]);
        update(heap, dp[color], color);
      }

      long ret = 0;
      for (int j = 1; j <= n; ++j) ret = Math.max(ret, dp[j]);
      out.println(ret);
    }

    out.close();
  }
  public static void main(String[] args) throws Exception {

    /*  BufferedReader br=new BufferedReader(new FileReader("input.txt"));
        BufferedWriter out=new BufferedWriter(new FileWriter("output.txt"));
    */
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 2000);
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out), 2000);
    String[] s = br.readLine().split(" ");
    int n = Integer.parseInt(s[0]);
    int q = Integer.parseInt(s[1]);
    int num[] = new int[n + 1];
    int[] m = new int[3 * n + 1]; // size = 2*n+1
    Arrays.fill(num, -1);
    s = br.readLine().split(" ");
    for (int i = 1; i <= n; i++) num[i] = Integer.parseInt(s[i - 1]);
    /// build tree
    maketree(1, 1, n, m, num);

    for (int qq = 1; qq <= q; qq++) {
      s = br.readLine().split(" ");
      int i = Integer.parseInt(s[0]);
      int j = Integer.parseInt(s[1]);
      int ans = query(1, 1, n, m, num, i, j);
      out.write("" + num[ans] + "\n");
      out.flush();
    }
  }
Exemplo n.º 8
0
 public void Init() {
   fail[0] = 0;
   flag[0] = 0;
   Arrays.fill(ch[0], 0);
   sz = 1;
   for (int i = 0; i < DIC.length(); i++) ID[DIC.charAt(i)] = i;
   CD = DIC.length();
 }
Exemplo n.º 9
0
 public static void solve() {
   int cases = scan.nextInt();
   scan.nextLine();
   for (int caze = 1; caze <= cases; caze++) {
     out.print("Case #" + caze + ": ");
     for (int i = 0; i < MAX; i++) Arrays.fill(memo[i], -1);
     str = scan.nextLine().toCharArray();
     out.println(go(0, str.length) ? "YES" : "NO");
   }
 }
Exemplo n.º 10
0
 public String[] dealHands(int numPlayers, String deck) {
   int playerId = 0;
   String r[] = new String[numPlayers];
   Arrays.fill(r, "");
   for (int i = 0; i < deck.length() - (deck.length() % numPlayers); i++) {
     r[playerId++] += deck.charAt(i);
     playerId %= numPlayers;
   }
   return r;
 }
Exemplo n.º 11
0
 public void Insert(String s) {
   int p = 0;
   for (int i = 0; i < s.length(); i++) {
     int c = ID[s.charAt(i)];
     if (ch[p][c] == 0) {
       Arrays.fill(ch[sz], 0);
       flag[sz] = 0;
       ch[p][c] = sz++;
     }
     p = ch[p][c];
   }
   flag[p] = 1;
 }
Exemplo n.º 12
0
 public DFA(char[] z, int k, int s, int[] t, int[][] f, boolean[][] g) {
   this.z = z.length;
   this.k = k;
   this.s = s;
   this.t = t.clone();
   e = new int[z.length][k];
   for (int i = 0; i < z.length; ++i) {
     Arrays.fill(e[i], -2);
     for (int j = 0; j < k; ++j) {
       if (e[i][j] == -2) {
         dfs(j, e[i], f[i], g[i]);
       }
     }
   }
 }
 public int bfs() {
   int[] record = new int[n];
   Arrays.fill(record, Integer.MAX_VALUE);
   LinkedList<Integer> queue = new LinkedList<Integer>();
   queue.addLast(0);
   int distance = 0;
   int count = 1;
   while (queue.size() > 0) {
     int newCount = 0;
     while (count > 0) {
       int tempPoint = queue.pollFirst();
       ArrayList<Road> tempRoads = roads[tempPoint];
     }
     distance++;
   }
 }
Exemplo n.º 14
0
  static void solve() {
    for (int i = 0; i < MAX; i++) choose[i][0] = choose[i][i] = 1;
    for (int i = 1; i < MAX; i++)
      for (int j = 1; j < i; j++) choose[i][j] = (choose[i - 1][j - 1] + choose[i - 1][j]) % MOD;
    for (int i = 0; i < MAX; i++) Arrays.fill(memo[i], -1);

    Scanner scan = new Scanner(in);
    int T = scan.nextInt();
    for (int t = 1; t <= T; t++) {
      out.print("Case #" + t + ": ");
      int n = scan.nextInt();
      int ways = 0;
      for (int i = 1; i < n; i++) ways = (ways + go(i, n)) % MOD;
      out.println(ways);
    }
  }
 public long getNumber(int m, int[] l, int[] r) {
   int[] belongs = new int[m];
   Arrays.fill(belongs, -1);
   int n = l.length;
   for (int i = 0; i < n; ++i) {
     for (int j = l[i] - 1; j < r[i]; ++j) {
       belongs[j] = i;
     }
   }
   long result = 1;
   boolean[] visited = new boolean[n];
   for (int i = 0; i < m; ++i) {
     if (belongs[i] != -1 && !visited[belongs[i]]) {
       result *= 2;
       visited[belongs[i]] = true;
     }
   }
   return result;
 }
  // sieve
  public static int[] primes(int n)
      throws Exception { // for(int i=1;i<=arr.length-1;i++)out.write(""+arr[i]+" ");
    boolean arr[] = new boolean[n + 1];
    Arrays.fill(arr, true);
    for (int i = 1; i <= Math.sqrt(n); i++) {
      if (!arr[i]) continue;
      for (int j = 2 * i; j <= n; j += i) {
        arr[i] = false;
      }
    }
    LinkedList<Integer> ll = new LinkedList<Integer>();
    for (int i = 1; i <= n; i++) {
      if (arr[i]) ll.add(i);
    }
    n = ll.size();

    int primes[] = new int[n + 1];
    for (int i = 1; i <= n; i++) {
      primes[i] = ll.removeFirst();
    }
    return primes;
  }
Exemplo n.º 17
0
  public Main() {
    try {
      in = new BufferedReader(new InputStreamReader(System.in));
      // minimum distance from D to K
      int numCities = nextInt();
      int tradeRoutes = nextInt();
      int[][] adjacencyMatrix = new int[numCities][numCities];
      int[] minDistance = new int[numCities];
      Arrays.fill(minDistance, 100000000);
      // Arrays.fill(adjacencyMatrix, -1);

      // int [] pencilCosts = new int[
      Node[] cities = new Node[numCities];
      for (int x = 0; x < tradeRoutes; x++) {
        int cityA = nextInt() - 1;
        int cityB = nextInt() - 1;
        int cost = nextInt();

        if (cities[cityA] == null) cities[cityA] = new Node(cityA);
        if (cities[cityB] == null) cities[cityB] = new Node(cityB);
        adjacencyMatrix[cityA][cityB] = cost;
        adjacencyMatrix[cityB][cityA] = cost;

        // cities[cityA].routes.add(new Edge(cost, cities[cityB]));
        // cities[cityB].routes.add(new Edge(cost, cities[cityA]));
      }

      int numStores = nextInt();
      int[] pencilCosts = new int[numCities];
      Arrays.fill(pencilCosts, -1);
      for (int x = 0; x < numStores; x++) {
        int ID = nextInt() - 1;
        int cost = nextInt();
        pencilCosts[ID] = cost;
      }
      int destination = nextInt() - 1;
      // if (isGood[destination]){

      // }
      int minCost = 100000000;

      Queue<Node> Q = new LinkedList<Node>();
      // PriorityQueue<Node> Q = new PriorityQueue<Node>();
      minDistance[destination] = 0;
      // cities[destination].distance = 0;
      Q.offer(cities[destination]);
      while (!Q.isEmpty()) {
        Node temp = Q.poll();
        for (int x = 0; x < numCities; x++) {
          if (adjacencyMatrix[temp.ID][x] != 0
              && (minDistance[x] == 100000000
                  || minDistance[x] > minDistance[temp.ID] + adjacencyMatrix[temp.ID][x])) {
            minDistance[x] = minDistance[temp.ID] + adjacencyMatrix[temp.ID][x];
            if (pencilCosts[x] != -1 && minDistance[x] < minCost) {
              // System.out.println(minCost);
              minCost = Math.min(minDistance[x] + pencilCosts[x], minCost);
              Q.offer(cities[x]);
            } else {
              if (pencilCosts[x] == -1) { // why>
                Q.offer(cities[x]);
              }
            }
            // Q.offer(temp.routes.get(x).destination);
          }
        }
      }

      for (int x = 0; x < numCities; x++) {
        if (pencilCosts[x] != -1
            && pencilCosts[x] + minDistance[x] < minCost
            && minDistance[x] != 100000000) {
          minCost = minDistance[x] + pencilCosts[x];
        }
      }
      System.out.println(minCost);

    } catch (IOException e) {
      System.out.println("IO: General");
    }
  }