美文网首页
uva140 STL枚举排列加剪枝

uva140 STL枚举排列加剪枝

作者: Amosasas | 来源:发表于2017-09-25 23:01 被阅读0次
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

const int maxn = 10;
const int INF = 0x7fffffff;

int main()
{
    char s[1000];
    while( scanf("%s", s) && s[0]!= '#')              
    {
        int n = 0;
        int id[300] = {};
        char letter[maxn] = {};
        for(char c= 'A'; c <= 'Z'; c++)
            if(strchr(s, c) != NULL)
            {
                id[c] = n++;
                letter[id[c]] = c;                        //建立双射数组 id: char - int  letter: int - char 
            }
        int p = 0, q = 0, len = strlen(s);
        
        vector<int> u, v;
        for(;;)  //使用 p q 定位 ';' 与 ';' 
        {
            while(s[p] != ':' && p < len)
              p++;
            if( p == len)                                //p位于q前 到底退出 
              break;
            while(s[q] != ';' && q < len)
              q++;
            for(int i = p+1; i < q; i++)
            {
                u.push_back(id[s[p-1]]);
                v.push_back(id[s[i]]);
            }
            p++;
            q++;                                          //继续移动 
        }
//      for(int i = 0; i < u.size(); i++)
//      {
//          cout<<u[i]<<" "<<v[i]<<endl;    
//      }  
        
        int Pos[maxn] = {}, P[maxn] = {}, bestP[maxn] = {};    //P[i] = j j放置在i上   Pos[i] = j i在位置j上 
        int bandwidth = 0, ans = INF;
        for(int i = 0; i < n; i++)
            P[i] = i;
        do{
            for(int i = 0; i < n; i++)
                Pos[P[i]] = i;
            bandwidth = 0;
            for(int i = 0; i < u.size(); i++)
            {
                bandwidth = max(bandwidth, abs(Pos[u[i]] - Pos[v[i]]));
                if(bandwidth > ans)
                    break;
            }
            if(bandwidth < ans)
            {
                ans = bandwidth;
                memcpy(bestP, P, sizeof(P));
            }
        }while(next_permutation(P, P+n));
        for(int i = 0; i < n; i++)
            printf("%c ", letter[bestP[i]]);
        printf("-> %d\n", ans);
    }
    return 0;
}

相关文章

  • uva140 STL枚举排列加剪枝

  • 《算法竞赛入门经典》第七章学习笔记

    枚举排列 生成1~n的排列 生成可重集的排列 利用STL生成排列 子集生成 增量构造法 位向量法 二进制法

  • 使用C++ STL的next/prev_permutation函

    使用C++ STL的next_permutation函数可以简单的枚举出一个升序排列的字符串的全排列,它包含在头文...

  • 枚举排列

    过程描述: 1->1,2->1,2,31,3->1,3,2当i =1 循环递归压栈在第二次的时候会有 1,2和1,...

  • 枚举排列

    生成1-n的排列 具体题目为,输入整数n,按字典序大小从小到大顺序输出前n个数的所有排列。例如输入n=3,则要求得...

  • AcWing 165. 小猫爬山(搜索)

    深度优先搜索(dfs) 体会 要考虑的问题 枚举对象 dfs的参数 返回条件 剪枝技巧原题链接 枚举对象: 车...

  • 递归-全排列

    对数组{1,2,3}进行全排列 STL next_permutation

  • STL之排列组合

    next_permutation() 正序 example 1,2,3 prev_permutation() ...

  • UVA 1592(Database)

    思路:参照紫书,先将字符串映射成数字便于处理,然后枚举每两列,再枚举每一行,利用STL里面的map做一个查找,若找...

  • leetcode指北---DFS

    DFS,也就是深搜,实质就是枚举。如果题目问的是一共多少种方法,多少种排列...尽管用。 全排列问题: 全排列:给...

网友评论

      本文标题:uva140 STL枚举排列加剪枝

      本文链接:https://www.haomeiwen.com/subject/bzrjextx.html