Блог пользователя hpfdf

Автор hpfdf, история, 8 лет назад, По-английски
#include <iostream>
#include <vector>
using namespace std;

vector<int> a;

int gen(int x) {
  if (!x)
    return 0;
  int u = a.size();
  a.push_back(0);        //*/
  a[u] = gen(x - 1) + 1; /*/
  int t = gen(x - 1) + 1;
  a[u] = t;              //*/
  return a[u];
}

int main() {
  gen(10);
  for (int x : a)
    cout << x << " ";
  cout << endl;
  return 0;
}

I recently had a non-trivial bug related to std vector. Above is the simplified code to reproduce it. Semantically this should output 10 9 8 7 6 5 4 3 2 1 but actually it doesn't. The problem was here a[u] = gen(x - 1) + 1. The vector a is reallocated before gen(x - 1) finish, so the target a[u] is outdated. Adding a temp variable t resolves this bug.

Summary: be careful playing with vectors. Never do a[i] = f(), where f() could modify vector a.

Полный текст и комментарии »

  • Проголосовать: нравится
  • +18
  • Проголосовать: не нравится

Автор hpfdf, 14 лет назад, По-английски
You're given matrices A and B, in which the elements are either 0 or 1.
The task is to determine whether A can change into B via swapping it's rows and columns.

How to solve this question...?

Полный текст и комментарии »

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

Автор hpfdf, 14 лет назад, По-английски
There are N integers, in range [0, M).
Select 2 distinct integers (p and q) from them, and maximize:

1. p OR q
2. p AND q
3. p XOR q
4. p NOR q

NOTE: M can be very large, so please take simple operating into efficient consideration. i.e. It takes O(logM) time to compute (p OR q)

Can anyone come out with solutions less than O(N^2logM) ?

Полный текст и комментарии »

  • Проголосовать: нравится
  • +1
  • Проголосовать: не нравится