解いた問題

5/15/2012

AOJ1251

AOJ1231

やるだけ。気合。



#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <sstream>

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>

#define REP(i, n) for(int i=0; i<(int)n; ++i)
#define FOR(i, c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(),(c).end()
#define each(i, c) FOR(i, c)

#define VAR(a) cout << #a << " : " << a << endl;

typedef long long int lli;

using namespace std;

class Dir {
public:
  map<string, Dir*> next;
  Dir() {}
  Dir(Dir* p)
  {
    next[".."] = p;
    next["."] = this;
  }
  ~Dir()
  {
    each (d, next) {
      if (d->first == ".") continue;
      if (d->first == "..") continue;
      delete d->second;
    }
  }
  void insert(istringstream &iss)
  {
    string s;
    iss >> s;
    if (!next.count(s)) next[s] = new Dir(this);
    if (s.size()) next[s]->insert(iss);
    else next.clear();
    return ;
  }
  Dir* find(string s)
  {
    replace(s.begin(), s.end(), '/', ' ');
    istringstream iss(s);
    vector<string> v;
    for (string t; iss >> t; v.push_back(t)) ;
    Dir* d = this;
    for (int i = 0; i < (int)v.size(); ++i) {
      if (d == NULL) return NULL;
      if (d->next.count(v[i]) == 0) return NULL;
      d = d->next[v[i]];
    }
    if (d && d->next.count("index.html")) {
      d = d->next["index.html"];
    }
    return d;
  }
  bool isFile(void)
  {
    return next.empty();
  }
};

int main(int argc, char *argv[])
{
  int n, m;
  while (cin >> n >> m && (n | m)) {
    Dir* root = new Dir(NULL);
    for (int i = 0; i < (int)n; ++i) {
      string s;
      cin >> s;
      replace(s.begin(), s.end(), '/', ' ');
      istringstream iss(s);
      root->insert(iss);
    }
    for (int i = 0; i < (int)m; ++i) {
      string a, b;
      cin >> a >> b;
      if (a[a.size() - 1] == '/') a += '.';
      if (b[b.size() - 1] == '/') b += '.';
      Dir* x = root->find(a);
      Dir* y = root->find(b);
      if (x == NULL || y == NULL) cout << "not found" << endl;
      else if (!x->isFile() || !y->isFile()) cout << "not found" << endl;
      else if (x == y) cout << "yes" << endl;
      else cout << "no" << endl;
    }
    delete root;
  }
  return 0;
}