2015年10月1日

GLFW callback member function


void Application::Initial() {
  window = glfwCreateWin....

  glfwSetWindowUserPointer(window, this);

  auto reshape_callback = [](GLFWwindow *window, int w, int h) {
    static_cast<Application *>(glfwGetWindowUserPointer(window))->Reshape(window, w, h);
  };


  glfwSetFramebufferSizeCallback(window, reshape_callback);
}

2014年10月6日

UVa 12467 - Secret Word

#include <cstdio>
#include <cstring>
#include <algorithm>
 
using namespace std;
 
const int MAXLEN = 1111111;
const int MOD = 10007;
const int C1 = 17, C2 = 23;
 
int c1[MAXLEN] = {1}, c2[MAXLEN] = {1};
 
char s1[MAXLEN], s2[MAXLEN];
int len;
 
int h1[MAXLEN], h2[MAXLEN];
int h3[MAXLEN], h4[MAXLEN];
 
void rev(char * s) {
  char * f = s, * t = s;
  while (*t) {
    t++;
  }
  t--;
  while (s < t) {
    char temp = *s;
    *s++ = *t;
    *t-- = temp;
  }
}
 
int hash1(int * h, int a, int b) {
  int result = (h[b] - (h[a - 1] * c1[b - a + 1]) % MOD + MOD) % MOD;
  return result;
}
 
int hash2(int * h, int a, int b) {
  int result = (h[b] - (h[a - 1] * c2[b - a + 1]) % MOD + MOD) % MOD;
  return result;
}

int LCP(int l, int lRev) {
  int ans = 0;
  int minLCP = 0, maxLCP = min(len - l + 1, len - lRev + 1);
  while (minLCP <= maxLCP) {
    int midLCP = (minLCP + maxLCP) / 2;
    int left = l, right = l + midLCP - 1;
    int leftRev = lRev, rightRev = leftRev + midLCP - 1;
    if ((hash1(h1, left, right) == hash1(h2, lRev, rightRev)
        && hash2(h3, left, right) == hash2(h4, lRev, rightRev))) {
      ans = max(ans, midLCP);
      minLCP = midLCP + 1;
    } else {
      maxLCP = midLCP - 1;
    }
  }
  return ans;
}
 
int main() {
  for (int i = 1; i < MAXLEN; i++) {
    c1[i] = (c1[i - 1] * C1) % MOD;
    c2[i] = (c2[i - 1] * C2) % MOD;
  }
  gets(s1);
  int T;
  sscanf(s1, "%d", &T);
  while (T--) {
    gets(s1 + 1);
    sprintf(s2 + 1, "%s", s1 + 1);
    rev(s2 + 1);
    len = strlen(s1 + 1);
    h1[0] = h2[0] = 0;
    h3[0] = h4[0] = 0;
    for (int i = 1; s1[i]; i++) {
      h1[i] = (s1[i] + (h1[i - 1] * C1) % MOD) % MOD;
      h2[i] = (s2[i] + (h2[i - 1] * C1) % MOD) % MOD;
 
      h3[i] = (s1[i] + (h3[i - 1] * C2) % MOD) % MOD;
      h4[i] = (s2[i] + (h4[i - 1] * C2) % MOD) % MOD;
    }
    int maxLCP = 0;
    for (int i = 1; s1[i] && (len - i + 1) > maxLCP; ++i) {
      maxLCP = max(maxLCP, LCP(1, i));
    }
    s1[maxLCP + 1] = 0;
    rev(s1 + 1);
    puts(s1 + 1);
  }
  return 0;
}

2014年7月7日

UVa 12488 - Start Grid

#include <stdio.h>

int main() {
  int N;
  while (scanf("%d", &N) == 1) {
    int grid[2][99];
    int i, j, k;
    for (i = 0; i < N; i++) {
      scanf("%d", &grid[0][i]);
    }
    for (i = 0; i < N; i++) {
      scanf("%d", &grid[1][i]);
    }
    int count = 0;
    for (i = N - 1; i >= 0; i--) {
      for (j = 0; j < N; j++) {
        if (grid[0][j] == grid[1][i]) {
          for (k = j; k < i; k++) {
            int temp = grid[0][k + 1];
            grid[0][k + 1] = grid[0][k];
            grid[0][k] = temp;
            count++;
          }
          break;
        }
      }
    }
    printf("%d\n", count);
  }
  return 0;
}

2014年7月6日

UVa 677 - All Walks of length "n" from the first node

#include <cstdio>
#include <vector>

using std::vector;

int N, L;
bool solved;

vector<int> con[10], path;

void solve(int n, int len, bool * visited) {
  if (len == L) {
    solved = true;
    for (int i = 0; i < path.size(); i++) {
      printf("%s%d%s", (i == 0) ? "(" : "", path[i] + 1, (i == path.size() - 1) ? ")" : ",");
    }
    puts("");
    return;
  }
  for (int i = 0; i < con[n].size(); i++) {
    int j = con[n][i];
    if (!visited[j]) {
      visited[j] = true;
      path.push_back(j);
      solve(j, len + 1, visited);
      path.pop_back();
      visited[j] = false;
    }
  }
}

int main() {
  int temp = 0;
  do {
    if (temp) {
      puts("");
    }
    scanf("%d%d", &N, &L);
    for (int r = 0; r < N; r++) {
      con[r].clear();
      for (int c = 0; c < N; c++) {
        int ok;
        scanf("%d", &ok);
        if (ok) {
          con[r].push_back(c);
        }
      }
    }
    solved = false;
    path.clear();
    path.push_back(0);
    bool visited[10] = {true};
    solve(0, 0, visited);
    if (!solved) {
      printf("no walk of length %d\n", L);
    }
  } while (scanf("%d", &temp) == 1);
  return 0;
}

UVa 12700 - Banglawash

#include <stdio.h>

int main() {
  int T, C = 1;
  scanf("%d", &T);
  while (T--) {
    int N;
    scanf("%d", &N);
    char s[99];
    scanf("%s", s);
    int i, cnt[99] = {};
    for (i = 0; s[i]; i++) {
      cnt[s[i]]++;
    }
    printf("Case %d: ", C++);
    if (cnt['A'] == N) {
      puts("ABANDONED");
    } else if ((cnt['B'] + cnt['A']) == N) {
      puts("BANGLAWASH");
    } else if ((cnt['W'] + cnt['A']) == N) {
      puts("WHITEWASH");
    } else if (cnt['B'] == cnt['W']) {
      printf("DRAW %d %d\n", cnt['B'], cnt['T']);
    } else if (cnt['B'] > cnt['W']) {
      printf("BANGLADESH %d - %d\n", cnt['B'], cnt['W']);
    } else {
      printf("WWW %d - %d\n", cnt['W'], cnt['B']);
    }
  }
  return 0;
}