Exemplo n.º 1
0
int fun(){
    ut i, t;
    Node one = {0}, next;
    tab.clear();
    for(i=0;i<SIZ;i++){
        cin>>t;
        if(t){
            one.m |= (1<<i);
            one.s++;
        } 
    }
    priority_queue<Node, vector<Node>, Node::cmp> q;
    q.push(one);
    while(!q.empty()){
        one=q.top(); q.pop();
        if(one.m == 0)
            break;
        for(i=0;i<SIZ;i++){
            next = one;
            next.o |= (1<<i);
            set(next,i);
            if(tab.find(next.m) == tab.end()){
                q.push(next);
                tab.insert(next.m);
            }
        }
    }
    output(one.o);
    return 0;
}
Exemplo n.º 2
0
int PE098()
{
    ifstream	fs;
    string		line;
    int			count = 0;

    fs.open("C:\\Users\\Karthik\\words2.txt", ifstream::in);
    getline(fs, line);
    istringstream ss(line);

    while (ss)
    {
        string		word, sig;
        if (!getline(ss, word, ','))
            break;
        sig = string_signature(word);
        if (anagrams.find(sig) != anagrams.end())
            anagrams[sig].push_back(word);
        else
        {
            vector<string> v(1, word);
            anagrams[sig] = v;
        }
    }
    for (long long i = 1; i * i <= MAX; i++)
        squares.insert(convert2string(i * i));
    for (map<string, vector<string> >::iterator it = anagrams.begin(); it != anagrams.end(); it++)
    {
        vector<string>		v = (*it).second;
        string				ret;

        if (v.size() == 2 && v[0].length() >= 4)
        {
            used.clear();
            if ("" != (ret = square_anagram(v[0], v[1])))
                cout << v[0] << "; " << v[1] << " == > " << ret << endl;
        }
    }
    fs.close();
    return 0;
}
int main(int argc, char** argv) {
  SatProblem cnf;
  cnf.parse(stdin);

  size_t count = 0;

  while (count < 100) {
    cerr << "count = " << count << endl;

    equivalences.clear();
    implications.clear();
    for (size_t i = 0; i < cnf.clauses.size(); ++i) {
      if (cnf.clauses[i].size() == 0) {
        abort();
      } else if (cnf.clauses[i].size() == 1) {
        addEquivalence(cnf.clauses[i][0], TRUE);
        addEquivalence(invert(cnf.clauses[i][0]), FALSE);
      } else if (cnf.clauses[i].size() == 2) {
        implications[invert(cnf.clauses[i][0])].push_back(cnf.clauses[i][1]);
        implications[invert(cnf.clauses[i][1])].push_back(cnf.clauses[i][0]);
      } else {
        // Do nothing
      }
    }

    done.clear();
    for (hash_map<Lit, vector<Lit> >::const_iterator i = implications.begin();
         i != implications.end(); ++i) {
      vector<Lit> stack;
      // cerr << "Exploring " << i->first << endl;
      explore(i->first, stack);
    }

    vector<Clause> oldClauses = cnf.clauses;
    cnf.clauses.clear();

    for (size_t i = 0; i < oldClauses.size(); ++i) {
      Clause cl = oldClauses[i];
      for (size_t j = 0; j < cl.size(); ++j) {
        cl[j] = normalize(cl[j]);
      }
      cnf.addClause(cl);
    }

    for (size_t i = 0; i < cnf.interfaceVariables.size(); ++i) {
      InterfaceVariable& iv = cnf.interfaceVariables[i];
      for (size_t j = 0; j < iv.second.size(); ++j) {
        Lit oldLit = iv.second[j];
        Lit newLit = normalize(oldLit);
        iv.second[j] = newLit;
      }
    }

    if (equivalences.empty()) break; // No changes

    ++count;
  }

  cnf.unparse(stdout);

  return 0;
}