Example #1
0
void nextPermutation(int level)
{
	if (level == VarLength)
	{
		print(); //Print
		return;
	}

	for (int j = 0; j < VarLength; j++)
	{
		bool IsValid = true;
		for (int k = 0; k < VarLength; k++)
		{
			if (OrderMatrix[Vars[j] - 'a'][Vars[k] - 'a'] == 1 && Visited[k] == 1)
			{
				IsValid = false;
				break;
			}
		}

		if (Visited[j] == 0 && IsValid)
		{
			Result[level] = Vars[j];
			Visited[j] = 1;
			nextPermutation(level + 1);
			Visited[j] = 0;
		}
	}
}
Example #2
0
    vector<string> generatePalindromes(string s) {
        vector<string> ans;

        map<int, int> ct; // get occurrence of each char.
        for (int i = 0; i < s.length(); ++ i) ct[s[i]] ++;

        vector<int> even, odd; // get chars that happen odd or even times.
        for (map<int, int>::iterator it = ct.begin(); it != ct.end(); ++ it) {
            if ((*it).second & 1) odd.push_back((*it).first);
            else even.push_back((*it).first);
        }

        char mid; // middle char when there are odd number of chars.
        if (s.length() & 1) {
            if (odd.size() != 1) return ans; // not possible.
            mid = odd[0];
        } else {
            if (odd.size() > 0) return ans;  // not possible.
        }

        // now generate palindromes.
        sort(even.begin(), even.end());
        do {
            string p = to_string(even), q = p;
            reverse(q.begin(), q.end());

            if (s.length() & 1) ans.push_back(p + mid + q);
            else ans.push_back(p + q);
        } while (nextPermutation(even));

        return ans;
    }
Example #3
0
int main ()
{
	srand(clock());
	std::vector<int> num;
	for (int i = 0 ; i < 10; i++)
	{
		int temp;
		temp = rand() % 10;
		num.push_back(temp);
	}
	for (int i = 0; i < num.size(); i++)
	{
		std::cout<<num[i];
	}

	std::cout<<std::endl;

	nextPermutation(num);
	for (int i = 0; i < num.size(); i++)
	{
		std::cout<<num[i];
	}
	system("pause");
	return 0;

}
Example #4
0
int main(int argc, char **argv)
{
	int d[] = {1,1};
	nextPermutation(d, 2);
	print(d, 2);
	return 0;
}
Example #5
0
    vector<string> generatePalindromes(string s) {
        vector<string> ans;
        if (s.length() == 1) ans.push_back(s); 
        if (s.length() <= 1) return ans;

        const int SIZE = 256;
        int ct[SIZE] = {0}, len = s.length();
        for (int i = 0; i < len; ++ i) ++ ct[s[i]];

        // count number of each char, 
        // and get the index of the char of odd occurrence.
        int odd = 0, odd_index = 0;
        for (int i = 0; i < SIZE; ++ i) {
            if (ct[i] & 1) {
                odd ++;
                odd_index = i;
            }
        }
        
        // iff can have palindromes, get such strings.
        if (((len & 1 == 1) && odd == 1) || (odd == 0)) {
            vector<int> nums = getChars(ct, SIZE); // get half of the chars.
            sort(nums.begin(), nums.end());
            
            do {
                ans.push_back(getPalindrome(nums, len & 1 == 1, odd_index));
            } while (nextPermutation(nums));
        }
        
        return ans;
    }
Example #6
0
 vector<vector<int>> permute(vector<int>& nums) {
     vector<vector<int> > res;
     sort(nums.begin(), nums.end());
     res.push_back(nums);
     while (nextPermutation(nums))
         res.push_back(nums);
     return res;
 }
Example #7
0
 vector<vector<int>> permuteUnique(vector<int>& nums) {
     vector<vector<int>> res;
     sort(nums.begin(), nums.end());
     do {
         res.push_back(vector<int>(nums.begin(), nums.end()));
     } while (nextPermutation(nums));
     return res;
 }
Example #8
0
File: 2207.c Project: saki45/OJ
void getPermutation(char str[][5]) {
    char *strtmp = "ABCDE";
    memcpy(str[0], strtmp, 5);
    int i;
    for(i=1; i<120; i++) {
        memcpy(str[i], str[i-1], 5);
        nextPermutation(str[i]);
    }
}
    vector<vector<int> > permuteUnique(vector<int> &num) {
		vector<vector<int> > ans;
		sort(num.begin(), num.end());
		ans.push_back(num);
		while(nextPermutation(num)) {
			ans.push_back(num);
		}
		return ans;
	}
Example #10
0
 vector<vector<int>> permute(vector<int>& nums) {
     int size = 1;
     for (int i=2; i<=nums.size(); i++) size *= i;
     vector<vector<int>> res = {nums};
     for (int i=1; i<size; i++) {
         nums = nextPermutation(nums);
         res.push_back(nums);
     }
     return res;
 }
Example #11
0
int main (int argc, char **argv) {
	int array[argc-1], i;
	for (i = 0; i < argc - 1; i++) 
		array[i] = atoi(argv[i+1]);
	nextPermutation (array, argc - 1);
	for (i = 0; i < argc - 1; i++)
		printf ("%d ", array[i]);
	printf ("\n");
	return 0;
}
 vector<vector<int>> permuteUnique(vector<int>& nums) {
     vector<vector<int>> list;
     vector<int> perm = nums;
     sort(perm.begin(), perm.end());
     
     list.push_back(perm);
     while (nextPermutation(perm)) list.push_back(perm);
     
     return list;
 }
    vector<vector<int>> permuteUnique(vector<int> &num) {
        vector<vector<int>> result;

        sort(num.begin(), num.end());
        do {
            result.push_back(num);
        } while(nextPermutation(num));

        return result;
    }
 vector<vector<int>> permuteUnique(vector<int>& nums) {
     vector<vector<int> > res;
     if (nums.size() == 0) return res;
     std::sort(nums.begin(), nums.end());
     res.push_back(nums);
     while(nextPermutation(nums)){
         res.push_back(nums);
     }
     //res.push_back(nums);
     return res;
 }
Example #15
0
void main()
{
	int nums[] = {5,4,7,5,3,2};
	int numsSize = 6;
	int i;
	nextPermutation(nums, numsSize);
	
	for(i=0; i<numsSize; i++){	
		printf("%d", nums[i]);
	}
}
std::string getPermutation(int n, int k)
{
    const std::string S("123456789");
    std::string str = S.substr(0, n);
    
    for (int i = 1; i < k; ++i)
    {
        nextPermutation(str.begin(), str.end());
    }
    
    return str;
}
Example #17
0
	vector<vector<int> > permute(vector<int> &num) {
		vector<vector<int>> v;
		if(num.size()>0) {
			sort(num.begin(),num.end());
			v.push_back(num);
			while(nextPermutation(num)){
				v.push_back(num);
			}
		}
		return v;
        
    }
    bool NextPermutation::test() {
        vector<int> perm1 = {1, 0, 2, 3};
        vector<int> perm2 = {1, 0, 3, 2};
        vector<int> perm3 = {0, 3, 2, 1};
        vector<int> perm4 = {3, 2, 1, 0};
        vector<int> perm5 = {6, 2, 1, 5, 4, 3, 0};
        vector<int> perm6 = {2, 1, 0, 1};
        vector<int> perm7 = {2, 1, 1, 3};
        vector<int> perm8 = {2, 3, 1, 1};
        vector<vector<int>> perms = {perm1, perm2, perm3, perm4, perm5, perm6, perm7, perm8};

        for (vector<int> perm : perms) {
            if (nextPermutationBruteForce(perm) != nextPermutation(perm)) {
                cout << "Should be: " << vec_to_string(nextPermutationBruteForce(perm)) << endl;
                cout << "Result: " << vec_to_string(nextPermutation(perm)) << endl;
                return false;
            }
        }

        return true;
    }
Example #19
0
TEST(NextPermutation, T2) {
	std::string output;
	testing::internal::CaptureStdout();

	std::vector<int> nums{ 3,2,1 };
	nextPermutation(nums);

	std::vector<int> res{ 1, 2,3 };

	output = testing::internal::GetCapturedStdout();

	ASSERT_EQ(nums, res);
}
int main(int argc, char** argv) {

    /*=========== commandline parsing ===========*/

    int c;
    char *name = argv[0];
    static struct option long_options[] = {
        {"help", no_argument, NULL, 'h'}
    };
    int option_index = 0;

    while ((c = getopt_long(argc, argv, "h", long_options, &option_index)) != -1) {
        switch (c) {
            case 'h':
                help(name);
                return EXIT_SUCCESS;
            case '?':
                usage(name);
                return EXIT_FAILURE;
            default:
                fprintf(stderr, "Illegal option %c.\n", c);
                usage(name);
                return EXIT_FAILURE;
        }
    }
    
    if (argc - optind != 1){
        usage(name);
        return (EXIT_FAILURE);
    }
    
    n = atoi(argv[optind]);
    
    if(n < 0 || n > 8*sizeof(bitset)){
        usage(name);
        return (EXIT_FAILURE);
    }
    
    prepare();
    
    storeParityVector();
    
    while(nextPermutation()){
        storeParityVector();
    }
    
    finish();

    
    return (EXIT_SUCCESS);
}
vector<vector<int> > permute(vector<int>& nums)
{
	vector<int> initNums = nums;
	vector<vector<int> > res;

	do
	{
		nextPermutation(nums);
		res.push_back(nums);

	} while (!isNotEqual(nums, initNums));

	return res;
}
Example #22
0
int main()
{
	char tmpInput[1000];
	bool flag = false;

	while (fgets(tmpInput, 1000, stdin))
	{
		init(); //Init

		for (int i = 0; tmpInput[i]; i++)
		{
			if (isValidVar(tmpInput[i]))
			{
				Vars[VarLength++] = tmpInput[i];
			}
		}

		std::sort(Vars, Vars + VarLength); //Sort

		fgets(tmpInput, 1000, stdin);
		for (int i = 0; tmpInput[i]; i++)
		{
			char first, second;
			
			while (!isValidVar(tmpInput[i])) i++;
			first = tmpInput[i];
			i++;

			while (!isValidVar(tmpInput[i])) i++;
			second = tmpInput[i];
			i++;

			OrderMatrix[first - 'a'][second - 'a'] = 1; // Set order

			if (tmpInput[i] == '\0' || tmpInput[i] == '\n')
				break;
		}

		if (flag)
			printf("\n");		
		flag = true;

		//Permute
		nextPermutation(0);
	}
		
	return 0;
}
Example #23
0
    vector<vector<int>> permuteUnique(vector<int>& nums) {
      vector<vector<int> > res;
      int size;
      size = nums.size();

      sort(nums.begin(),nums.end());
      if (size == 1) {
	res.push_back(nums);
	return res;
      }
      res.push_back(nums);
      while (nextPermutation(nums) != 1)
	res.push_back(nums);

      return res;
    }
Example #24
0
 string getPermutation(int n, int k) {
     long long p = 1;
     vector<int> tmp(n);
     for (int i = 1; i <= n; ++i) {
         p *= i ;
         tmp[i] = i; 
     }
     for (int i = 1; i < k; i += 1){
         nextPermutation(tmp);
     } 
     string res;
     for (int i = 0; i < n; ++i) {
         res += (char)tmp[i] + '0';
     }
     return res;
 }
Example #25
0
char* getPermutation(int n, int k)
{
    if (n <= 0 || k <= 0)
        return NULL;

    char *s = (char*)malloc((n + 1) * sizeof(char));
    s[n] = '\0';
    for (int i = 0; i < n; i++)
    {
        s[i] = i + 1 + '0';
    }

    for (int i = 0; i < k - 1; i++)
        if (!nextPermutation(s))
            return NULL;

    return s;
}
Example #26
0
int main() {
    int nums[] = { 1, 2, 3, 4 };
    int size = sizeof(nums) / sizeof(nums[0]);
    int i = size;
    int count = 1;
    while (i) {
        count *= i;
        i--;
    }

    count++; /* test lowest possible order case */
    while (count--){
        for (i = 0; i < size; i++){
            printf("%d ", nums[i]);
        }
        printf("\n");
        nextPermutation(nums, size);
    }

    return 0;
}
Example #27
0
    vector<vector<int> > subsets(vector<int> &S) {
        int len = S.size();
        int cur;
        vector<int> v;
        vector<vector<int> > v_res;
		//先push一个空集合,因为即使S是空集合,那么v_res也至少应该包含一个空集合
        v_res.push_back(v);
        sort(S.begin(), S.end());//对S排序,保证结果有序
        for(int i=1; i<=len; i++){
            cur = (1<<i)-1; //第一个组合
            do{
                for(int j=0; j<len; j++){
                    if(cur&(0x1<<j)){
                        v.push_back(S[j]);
                    }
                }
				v_res.push_back(v);
				v.clear();  
            } while(cur = nextPermutation(len, cur));
        }
        return v_res;
    }
void scoring::MarkovNetworkScoreCalculator::calculateScores_internal(FloatMap &cache) {

    for (int layer = 1; layer <= maxClique && !outOfTime; layer++) {

        VARSET_NEW(clique, scoreCache.getVariableCount());
        for (int i = 0; i < layer; i++) {
            VARSET_SET(clique, i);
        }

        VARSET_NEW(max, variableCount);
        VARSET_SET(max, variableCount);

        while (VARSET_LESS_THAN(clique, max) && !outOfTime) {
            float score = scoringFunction->calculateScore(clique, cache);

#ifdef DEBUG
            printf("score, %" PRIu64 ": %f\n", clique, score);
#endif
            cache[clique] = score;

            // find the next combination
            clique = nextPermutation(clique);
        }

        if (!outOfTime) highestCompletedLayer = layer;
    }

    t->cancel();

#ifdef DEBUG
    printf("Scores\n");
    for (auto it = cache.begin(); it != cache.end(); it++) {
        printf("%" PRIu64 ": %f\n", (*it).first, (*it).second);
    }
#endif
}
Example #29
0
int main(void)
{
	int	x[] = { 1, 1, 5 };
	nextPermutation(x, 3);
	return(0);
}
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  uint32_t guessIdx = 0;
  int running = 0;
  int delay = 0;
  RCC_ClocksTypeDef RCC_Clocks;
  
  /* Initialize LEDs and User_Button on STM32F4-Discovery --------------------*/
  STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI); 
  
  STM_EVAL_LEDInit(LED4);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED5);
  STM_EVAL_LEDInit(LED6);
  
  /* SysTick end of count event each 10ms */
  RCC_GetClocksFreq(&RCC_Clocks);
  SysTick_Config(RCC_Clocks.HCLK_Frequency / 100);
  STM_EVAL_LEDOff(LED4);
  STM_EVAL_LEDOn(LED3);
  STM_EVAL_LEDOff(LED5);
  STM_EVAL_LEDOff(LED6);
  STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_GPIO);
  keyboardInit(&USB_OTG_dev);
  Demo_USBConfig();
  char guess[7];
  strcpy(guess, "400000");
  Delay(2000);
  while (1) {
    if (STM_EVAL_PBGetState(BUTTON_USER) == Bit_SET) {
      //crappy debounce routine
      TimingDelay = 10;
      while ((STM_EVAL_PBGetState(BUTTON_USER) == Bit_SET)&&(TimingDelay != 0x00));
      //now change start or stop password attempts
      if (running == 0) {
        STM_EVAL_LEDOn(LED4);
        running = 1;
      } else {
        STM_EVAL_LEDOff(LED4);
        running = 0;
      }
    }
    //mostly non blocking delay to allow stopping with button
    if (delay > 0) {
      Delay(1000);
      delay--;
    }
    if (running != 0 && delay == 0) {
      Delay(200);
      keyboardWrite(KEY_BACKSPACE);
      keyboardWrite(KEY_BACKSPACE);
      keyboardWrite(KEY_BACKSPACE);
      keyboardWrite(KEY_BACKSPACE);
      STM_EVAL_LEDToggle(LED6);
      keyboardPutString(guess);
      keyboardWrite(KEY_RETURN);
      Delay(200);
      keyboardWrite(KEY_RETURN);
      
      nextPermutation(guess, "123", 1);
      
      if ((++guessIdx % 5) == 0) {
        //try to email every 5 guesses
        keyboardReleaseAll();
        keyboardPress(KEY_LEFT_GUI);
        keyboardPress('g');
        Delay(50);
        keyboardReleaseAll();
        keyboardPutString("*****@*****.**"); //leave the preceding 'c' that is the gmail compose shortcut
        keyboardWrite(KEY_TAB);
        keyboardPutString(guess);
        keyboardWrite(KEY_TAB);
        keyboardPutString(guess);
        keyboardWrite(KEY_TAB);
        keyboardWrite(KEY_TAB);
        keyboardWrite(KEY_RETURN);
        STM_EVAL_LEDOff(LED5);
        delay = 30;
      }
    }
  }
}