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;
    }
    

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

  • 相关阅读:
    docker 启动redis
    mysql主从库搭建
    云镜象下载地址整理
    linux 命令积累
    canal 踩坑实录---这可能是你看到的最全最简单的canal教程
    数据库查询超级慢,数据库死锁的查看与解决
    微信公众平台开发---建立服务器与微信公众平台的链接
    使用sql更改表的列的数据类型和添加新列和约束
    Mac安装、配置MongoDB
    shell 变量
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/7828839.html
Copyright © 2011-2022 走看看