// Stores the subsets of size k of {0,...,n-1} in T,
// starting in position p, and returns p plus their number.
int MSTVerifier::subsets(int n,int k,int p, std::vector<int> T) {
  if (n<k) return p;
  if (k==0) { T[p]=0; return p+1; }
  int q=subsets(n-1,k-1,p,T);
  for (int i=p;i<q;i++) T[i]|=1<<(n-1);
  return subsets(n-1,k,q,T);
  }//end subsets
示例#2
0
int main()
{
    std::string test1{"123"};
    subsets(test1);
    std::string test2{"abcde"};
    subsets(test2);
    return 0;
}
示例#3
0
	void subsets(vector<int> nums, vector<int> tmp, vector<vector<int>>& ret, int i) {
		if(i == nums.size()) {
			ret.push_back(tmp);
			return;
		}
		subsets(nums, tmp, ret, i + 1);
		tmp.push_back(nums[i]);
		subsets(nums, tmp, ret, i + 1);
		return ;
	}
示例#4
0
void Solution078::subsets(const vector<int> &S, vector<int> &path, int step, vector<vector<int> > &result)
{
    if(step == S.size())
    {
        result.push_back(path);
        return;
    }
    subsets(S,path,step+1,result);
    path.push_back(S[step]);
    subsets(S,path,step+1,result);
    path.pop_back();
}
示例#5
0
 void subsets(vector<int> &S, int k, vector<int> &sub, vector<vector<int> > &result) {
     int n = S.size();
     if (k == n) {
         result.push_back(sub);
         return;
     }
    
     sub.push_back(S[k]);
     subsets(S, k+1, sub, result);
     sub.pop_back();
     subsets(S, k+1, sub, result);
 }
示例#6
0
 void subsets(vector<int> &S, int step, vector<int> &path, vector<vector<int> > &result) {
     if (step == S.size()) {
         result.push_back(path);
         return;
     }
     
     // don't choose S[step]
     subsets(S, step + 1, path, result);
     // choose S[step]
     path.push_back(S[step]);
     subsets(S, step + 1, path, result);
     path.pop_back();  // clear S[step]
     return;
 }
示例#7
0
 vector<VI> subsets(VI &S) {
     // Start typing your C/C++ solution below
     // DO NOT write int main() function
     
     vector<VI> result;
     if (S.size() == 0) {
         result.push_back(VI());
         return result;
     }
     
     int first = S.front();
     VI child(S.begin() + 1, S.end());
     vector<VI> subsetsChild = subsets(child);
     
     vector<VI>::iterator iter = subsetsChild.begin();
     while (iter < subsetsChild.end()) {
         VI &set = *iter;
         sort(set.begin(), set.end());
         result.push_back(set);
         
         set.push_back(first);
         sort(set.begin(), set.end());
         result.push_back(set);
         
         iter++;
     }
     
     return result;
 }
示例#8
0
int main(void)
{
	int **res, *col, n;
	int x[] = { 1, 2, 3};
	res = subsets(x, 3, &col, &n);
	return(0);
}
示例#9
0
 vector<vector<int> > subsets(vector<int> &S) {
     sort(S.begin(), S.end());
     vector<vector<int> > result;
     vector<int> path;
     subsets(S, 0, path, result);
     return result;
 }
示例#10
0
	vector<vector<int> > subsets(vector<int> &S)
	{
		vector<int> SS = S;
		sort(SS.begin(), SS.end());
		vector<vector<int> > result;
		
		
		int n = SS.size();
		if(n)
		{
			int nNum = SS[n-1];
			vector<int> S1 = SS;
			S1.resize(n-1);
			
			vector<vector<int> > res = subsets(S1);
			for(int i=0; i<res.size(); ++i)
			{
				vector<int> item = res[i];
				if(item.size())
					result.push_back(item);
					
				item.push_back(nNum);
				result.push_back(item);
			}
		}
		
		vector<int> null;
		result.push_back(null);
		return result;
	}
示例#11
0
std::vector<int> MSTVerifier::median_table(int h) {
   std::vector<int> T= std::vector<int>((1<<h)+1, -1),median= std::vector<int>(1<<h+1, -1);
    for (int s=0;s<=h;s++) {
      for (int k=0;k<=s;k++) {
	int p=subsets(h-s,k,0, T);
	int q=subsets(s,k,p, T);
	q=subsets(s,k+1,q, T);
	for (int i=0;i<p;i++) {
	  int b=(1<<s+1)*T[i]+(1<<s); // fixed high bits
	  for (int j=p;j<q;j++)
	    median[b+T[j]]=s; // variable low bits
	}
      }
    }
  return median;
} // end median_table
示例#12
0
 vector<vector<int> > subsets(vector<int> &S) {
     sort(S.begin(), S.end()); // 输出要求有序
     vector<vector<int> > result;
     vector<bool> selected(S.size(), false);
     subsets(S, selected, 0, result);
     return result;
 }
示例#13
0
 void subsets(vector<int> &S, int item_id, vector<int> &sub, vector<vector<int> > &result) {
     if (item_id == S.size()) {
         result.push_back(sub);
         return;
     }
     if (sub.size() && S[item_id] == sub.back()) {
         sub.push_back(S[item_id]);
         subsets(S, item_id + 1, sub, result);
         sub.pop_back();
     } else {
         subsets(S, item_id + 1, sub, result);
         sub.push_back(S[item_id]);
         subsets(S, item_id + 1, sub, result);
         sub.pop_back();
     }
 }
示例#14
0
vector< vector<int> > Solution078::subsets(vector<int> &nums)
{
    sort(nums.begin(),nums.end());
    vector<vector<int>> result;
    vector<int> path;
    subsets(nums,path,0,result);
    return result;
}
示例#15
0
文件: subset.cpp 项目: rumpelt/algo
int main(int argc, char* argv[])  {
  std::string test(argv[1]);
  std::vector<std::string> result;
  std::cout << "Generating Subset of string " << test << std::endl;
  subsets(test, result);
  result = permutation(test);
  printVector(result);
}
示例#16
0
 vector<vector<int> > subsetsWithDup(vector<int> &S) {
     vector<int> sub;
     vector<vector<int> > result;
     sort(S.begin(), S.end());
     subsets(S, 0, sub, result);
     
     return result;
 }
示例#17
0
文件: subset.cpp 项目: Kolkir/algo
void subsets_test()
{
	std::vector<int> m = {1, 2, 3};
	subsetcb(m);
	std::cout << std::endl;
	
	subsets(m.begin(), m.end(), subsetcb);
}
 static void subsets(const vector<int> &S, vector<bool> &selected, int step,
         vector<vector<int> > &result) {
     if (step == S.size()) {
         vector<int> subset;
         for (int i = 0; i < S.size(); i++) {
             if (selected[i]) subset.push_back(S[i]);
         }
         result.push_back(subset);
         return;
     }
     // 不选S[step]
     selected[step] = false;
     subsets(S, selected, step + 1, result);
     // 选S[step]
     selected[step] = true;
     subsets(S, selected, step + 1, result);
 }
示例#19
0
 vector<vector<int> > subsetsWithDup(const vector<int> &S) {
     // write your code here
     vector<int> v(S);
     sort(v.begin(), v.end());
     vector<int> vec;
     subsets(0, vec, v);
     return results;
 }
示例#20
0
void Modules::subsets(string soFar, string rest, char out, ofstream &outFile) {
    if (rest == "") {	// convert char symbols from the sub-strings of soFar to pitches, then print out
        for (int i = 0; i < static_cast<int>(soFar.length()); i++) {
            cout << TABLE[charToNum(soFar[i])];
            if (out == 'y')
                outFile << TABLE[charToNum(soFar[i])];
        }
        cout << endl;
        if (out == 'y')
            outFile << endl;
    } else {
        // add to subset, remove from rest, recursion
        subsets(soFar + rest[0], rest.substr(1), out, outFile);
        // don't add to subset, remove from rest, recursion
        subsets(soFar, rest.substr(1), out, outFile);
    }
}
示例#21
0
文件: subsets.cpp 项目: dalanlan/algo
 void subsets(vector<int>& nums, vector<vector<int>>& res, vector<int>& path, int index) {
     res.push_back(path);
     
     for(int i=index; i<nums.size(); i++) {
     
         path.push_back(nums[i]);
         subsets(nums, res, path, i+1);
         path.pop_back();
     }
 }
示例#22
0
  bool AnimeshFactoryLoader::ParseMorphTarget (iDocumentNode* child,
					       CS::Mesh::iAnimatedMeshFactory* amfact)
  {
    const char* name = child->GetAttributeValue ("name");
    CS::Mesh::iAnimatedMeshMorphTarget* morphTarget = amfact->CreateMorphTarget (name);

    csRef<iRenderBuffer> offsetsBuffer;

    csRef<iDocumentNodeIterator> it = child->GetNodes ();
    while (it->HasNext ())
    {
      csRef<iDocumentNode> child2 = it->Next ();
      if (child2->GetType () != CS_NODE_ELEMENT) continue;
      const char* value = child2->GetValue ();
      csStringID id = xmltokens.Request (value);
      switch (id)
      {
      case XMLTOKEN_SETS:
	{
	  const char* subsetIndices = child2->GetAttributeValue ("ids");
	  if (subsetIndices == 0)
	  {
	    synldr->ReportError (msgidFactory, child2, 
				 "No subset list defined while creating morph target");
	    return false;
	  }

	  csStringArray subsets (subsetIndices, " ");
	  for (size_t i = 0; i < subsets.GetSize (); i++)
	    morphTarget->AddSubset (atoi(subsets[i]));
	
	}
	break;
      case XMLTOKEN_OFFSETS:
	{
	  offsetsBuffer = synldr->ParseRenderBuffer (child2);
	  if (!offsetsBuffer)
	  {
	    synldr->ReportError (msgidFactory, child2, "Could not parse offsets buffer!");
	    return false;
	  }
	  morphTarget->SetVertexOffsets (offsetsBuffer);
	  morphTarget->Invalidate();
	}
	break;
      default:
	synldr->ReportBadToken (child2);
	return false;
      }
    }

    return true;
  }
示例#23
0
 void subsets(int pos, vector<int> &curSet, const vector<int> &S)
 {
     results.push_back(curSet);
     for (int i = pos; i < S.size(); ++i)
     {
         if (i != pos && S[i - 1] == S[i])
             continue;
         curSet.push_back(S[i]);
         subsets(i + 1, curSet, S);
         curSet.pop_back();
     }
 }
int main()
{


   vector<int> nums;
   int i=0;
   for(;i<15;i++)
	   nums.push_back(rand()%100);
   vector<vector<int> >res=subsets(nums);
   for(i=0;i<(signed)res.size();i++)
	   printVec(res[i]);
	return 0;
}
示例#25
0
bool Part::contains(const Part& part) const
{
    if (this == &part) { // same part
        return true;
    }
    const PartVector & subs = subsets();
    for (size_t i = 0, ie = subs.size(); i < ie; ++i) {
        if (subs[i] == &part) {
            return true;
        }
    }
    return false;
}
示例#26
0
int main() {
    int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int numsSize = sizeof(nums) / sizeof(int), *columnSizes, returnSize;
    int **ans = subsets(nums, numsSize, &columnSizes, &returnSize);
    int i, j;
    for (i = 0; i < returnSize; ++i) {
        for (j = 0; j < columnSizes[i]; ++j) {
            printf("%d ", ans[i][j]);
        }
        printf("\n");
    }
    system("pause");
    return 0;
}
示例#27
0
int main() {
    int nums[] = {2, 4, 3, 1, 5};
    int *colsz, retsz;
    int **r = subsets(nums, COUNT_OF(nums), &colsz, &retsz);
    int i;
    for (i = 0; i != retsz; i++) {
        printf("%3d: ", i);
        showArr(r[i], colsz[i]);
        free(r[i]);
    }
    free(colsz);
    free(r);
    return 0;
}
示例#28
0
 vector<vector<int> > subsets(vector<int> &S) {
     // Start typing your C/C++ solution below
     // DO NOT write int main() function
     sort(S.begin(), S.end());
     vector<vector<int> > result;
     if (S.size() == 0) {
         result.push_back(vector<int>());
         return result;
     }
     
     vector<int > sub;
     subsets(S, 0, sub, result);
     
     return result;
 }
示例#29
0
 vector<vector<int>> combine(int n, int k) {
     vector<vector<int>> res;
     if(n<1) return res;
     vector<int> nums;
     for(int i = 1; i <= n; ++i){
         nums.push_back(i);
     }
     if(n < k) return res;
     else if(n == k){
         res.push_back(nums);
         return res;
     }
     res = subsets(nums);
     auto it = partition(res.begin(),res.end(),[&k](vector<int> &v){return v.size() == k;});
     res.erase(it,res.end());
     return res;
 }
示例#30
0
void main() {
	
	int size, i;
	int *arr;

	printf("Enter the number of elements: ");
	scanf("%d", &size);
	
	arr = (int*) malloc(size* sizeof(int));
	
	printf("\nEnter the numbers: ");
	for (i = 0; i < size; i++) {
		scanf("%d", &arr[i]);
	}
	
	printf("\nThe subsets are: \n");
	subsets(arr, size);
}