zoukankan      html  css  js  c++  java
  • uva-10098

       所有的排列,但是要不重复

    #include<stdio.h>
    #include<iostream>
    #include<sstream>
    #include<queue>
    #include<map>
    #include<memory.h>
    #include <math.h>
    #include<time.h>
    #include <stdlib.h>
    #include <algorithm>
    using namespace std;
    #define N 12
    int vis[N];
    char a[N];
    char b[N];
    int length;
    int n;
    int final = 0;
    void dfs(int cur)
    {
    	if(cur == length)
    	{
    		for(int i = 0; i < cur; i++)
    			cout << b[i];
    		cout << endl;
    		return;
    	}
    	for(int i = 0; i < length; i++)
    	{
    		if(!i || a[i] != a[i - 1])
    		{
    			int c1 = 0, c2 = 0;
    			for(int j = 0; j < cur; j++)
    				if(b[j] == a[i])
    					c1++;
    
    			for(int j = 0; j < length; j++)
    				if(a[j] == a[i])
    					c2++;
    			if(c1 < c2)
    			{
    				b[cur] = a[i];
    				dfs(cur + 1);
    			}
    		}
    
    	}
    }
    void sort()
    {
    	for(int i = 0; i < length; i++)
    	{
    		for(int j = 1; j < length - i; j++)
    		{
    			if(a[j] < a[j - 1])
    			{
    				char cc = a[j];
    				a[j] = a[j - 1];
    				a[j - 1] = cc;
    			}
    		}
    	}
    
    }
    int main(const int argc, char** argv)
    {
    	freopen("d:\1.txt", "r", stdin);
    	cin >> n;
    	while (n--)
    	{
    		memset(a, 0, sizeof(a));
    		scanf("%s", a);
    		length = strlen(a);
    		sort();
    		dfs(0);
    		//if(n != 0)
    			cout << endl;
    	}
    	return 0;
    }
    

      

    #include<stdio.h>
    #include<iostream>
    #include<sstream>
    #include<queue>
    #include<map>
    #include<memory.h>
    #include <math.h>
    #include<time.h>
    #include <stdlib.h>
    #include <algorithm>
    using namespace std;
    #define N 12
    int vis[N];
    char a[N];
    char b[N];
    int length;
    int n;
    int final = 0;
    void dfs(int cur)
    {
    	if(cur == length)
    	{
    		for(int i = 0; i < cur; i++)
    			cout << b[i];
    		cout << endl;
    		return;
    	}
    	for(int i = 0; i < length; i++)
    	{
    		if(!i || a[i] != a[i - 1])
    		{
    			int c1 = 0, c2 = 0;
    			for(int j = 0; j < cur; j++)
    				if(b[j] == a[i])
    					c1++;
    
    			for(int j = 0; j < length; j++)
    				if(a[j] == a[i])
    					c2++;
    			if(c1 < c2)
    			{
    				b[cur] = a[i];
    				dfs(cur + 1);
    			}
    		}
    
    	}
    }
    void sort()
    {
    	for(int i = 0; i < length; i++)
    	{
    		for(int j = 1; j < length - i; j++)
    		{
    			if(a[j] < a[j - 1])
    			{
    				char cc = a[j];
    				a[j] = a[j - 1];
    				a[j - 1] = cc;
    			}
    		}
    	}
    
    }
    int main(const int argc, char** argv)
    {
    	freopen("d:\1.txt", "r", stdin);
    	cin >> n;
    	while (n--)
    	{
    		memset(a, 0, sizeof(a));
    		scanf("%s", a);
    		length = strlen(a);
    		sort();
    		do
    		{
    			cout << a << endl;
    		} while (next_permutation(a, a + length));
    		cout << endl;
    	}
    	return 0;
    }
    

      想看库函数源码了。。。。。

  • 相关阅读:
    Maven配置始终获取最新版本
    使用SpringBoot
    SpringBoot文档综述
    35_方法的使用
    34_结构体指针类型的使用
    33_同名字段的使用
    32_匿名字段的使用
    31_结构体的使用
    30_map的使用
    29_猜字游戏
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/7828839.html
Copyright © 2011-2022 走看看