void printStr(const unordered_set<string> &test_set,const int flag){
    if (flag==1) {
        cout << "nterminal string is :" << endl;
    }
    else{
        cout << "terminal string is :" << endl;
    }
    
    for (auto ptr = test_set.cbegin(); ptr!=test_set.cend(); ++ptr) {
        cout << *ptr <<endl;
    }
    cout<<endl;
}
Exemplo n.º 2
0
string check_ans(const string &s, unordered_set<string> &D) {
  int len = 0;
  for (auto iter = D.cbegin(); iter != D.cend(); ++iter) {
    int i;
    for (i = 0; i < s.size() && i < (*iter).size(); ++i) {
      if (s[i] != (*iter)[i]) {
        break;
      }
    }
    if (i > len) {
      len = i;
    }
  }
  if (len == s.size()) {
    return string();
  } else {
    return s.substr(0, len + 1);
  }
}
Exemplo n.º 3
0
//--------------------------- PRIVATE: extendSubgraph --------------------------
// Recursively looking size-k subgraphs of the graph.
// Precondition: The graph should have already been built or exists
// Postcondition: The list of subgraphs are displayed
void Graph::extendSubgraph(list<int> &Vsubgraph, unordered_set<int> &Vextension, unordered_set<int> &visited, const int &v, const int &k)
{
    if(Vsubgraph.size() == k-1)
    {
        for(int w : Vextension)
            count++;
        
        return;
    }
    
    list<int> unvisit;
    
    while(Vextension.size() != 0)
    {
        int w = *Vextension.cbegin();
        Vextension.erase(w);
        
        Vsubgraph.push_back(w);
        visited.insert(w);
        
        unvisit.push_back(w);
        
        unordered_set<int> Vextension2 = unordered_set<int>(Vextension);
        
        for (int vertex : vertices[w])
        {
            if (vertex > v && visited.count(vertex) == 0 && Vextension.count(vertex) == 0)
                Vextension2.insert(vertex);
        }
        
        extendSubgraph(Vsubgraph, Vextension2, visited, v, k);
        Vsubgraph.pop_back();
    }

    for(int i : unvisit)
        visited.erase(i);

}
//static
void TruthTableUtils::pickUpBestOutputValues(TruthTable* tablePtr, unordered_set<word> inputs,
    unordered_set<word> outputs)
{
    TruthTable& table = *tablePtr;

    // tuning option
    const ProgramOptions& options = ProgramOptions::get();
    bool pickUpBestOutputOnlyByHammingDistance = options.isTuningEnabled &&
        options.options.getBool("pick-up-best-output-only-by-hamming-distance", false);

    InputToBestIndexMap inputToBestIndexMap;
    deque<DistanceSum> emptyDeque;

    for (auto input : inputs)
        inputToBestIndexMap[input] = emptyDeque;

    while (inputs.size())
    {
        if (outputs.size() == 1)
        {
            assert(inputs.size() == 1,
                string("TruthTableUtils::pickUpBestOutputValues(): not all outputs were assigned correctly"));

            word input  = *inputs.cbegin();
            word output = *outputs.cbegin();

            table[input] = output;
            break;
        }

        updateBestIndicesForInput(&inputToBestIndexMap, inputs, outputs);

        word first = *(inputs.cbegin());
        word bestOutput = inputToBestIndexMap[first].front().index;

        word bestInput = first;
        if (pickUpBestOutputOnlyByHammingDistance)
        {
            // choose best index
            int minSum = inputToBestIndexMap[first].front().sum;
            for (auto input : inputs)
            {
                deque<DistanceSum>& sum = inputToBestIndexMap[input];
                if (sum.front().index == bestOutput)
                {
                    int currentSum = sum.front().sum;
                    if (currentSum < minSum)
                    {
                        minSum = currentSum;
                        bestInput = input;
                    }
                }
            }
        }
        else // !pickUpBestOutputOnlyByHammingDistance
        {
            // find other inputs with the same best pair index
            int totalSum = 0;
            for (auto input : inputs)
            {
                deque<DistanceSum>& sum = inputToBestIndexMap[input];

                if (sum.front().index == bestOutput)
                    totalSum += sum.back().sum;
            }

            // choose best index
            int minSum = totalSum - inputToBestIndexMap[first].back().sum;

            for (auto input : inputs)
            {
                deque<DistanceSum>& sum = inputToBestIndexMap[input];

                if (sum.front().index == bestOutput)
                {
                    int currentSum = totalSum - sum.back().sum;
                    if (currentSum < minSum)
                    {
                        minSum = currentSum;
                        bestInput = input;
                    }
                }
            }
        }

        // assign output for best input
        table[bestInput] = bestOutput;

        // clear map
        for (auto iter : inputToBestIndexMap)
        {
            deque<DistanceSum>& sum = inputToBestIndexMap[iter.first];
            
            if (sum.front().index == bestOutput)
                sum.pop_front();

            if (sum.back().index == bestOutput)
                sum.pop_back();
        }

        inputToBestIndexMap.erase(bestInput);

        // clear inputs and outputs
        inputs.erase(bestInput);
        outputs.erase(bestOutput);
    }
}
void printProdcution(const string &str, const unordered_set<string> &test_prdtion){
    for (auto ptr = test_prdtion.cbegin(); ptr!= test_prdtion.cend(); ++ptr) {
        cout << str << "-->" <<*ptr<<endl;
    }
    cout<<endl;
}