zoukankan      html  css  js  c++  java
  • POJ 1146 ID Codes (UVA146)

    // 求下一个排列
    // 如果已经是最后一个排列
    // 就输出 No Successor
    // stl 或 自己写个 生成排列 我测试了下 两个速率是一样的、只是代码长度不同

    /*
    #include <iostream>
    #include <string>
    #include<sstream>
    #include <cmath>
    #include <map>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    char s[100];
    int n;
    int main()
    {
    
       while(scanf("%s",s),strcmp(s,"#"))
       {
           n=strlen(s);
           if(next_permutation(s,s+n))
             printf("%s
    ",s);
           else
             printf("No Successor
    ");
       }
       return 0;
    }
    
    */
    
    #include <iostream>
    #include <string>
    #include<sstream>
    #include <cmath>
    #include <map>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    char s[100];
    int n;
    void change(int l,int r)
    {
         while(l<r)
         {
             swap(s[l],s[r]);
             l++;
             r--;
         }
    }
     bool permutation()
     {
         int i=n-1;
         while(i>0&&s[i-1]>=s[i]) i--;
         if(!i) return false;
         int k=i,j=n-1;
         for(;j>i;j--)
          if(s[j]>s[i-1]){
              k=j;
              break;
         }
         swap(s[i-1],s[k]);
         change(i,n-1);
         return true;
     }
    int main()
    {
    
       while(scanf("%s",s),strcmp(s,"#"))
       {
           n=strlen(s);
           if(permutation())
             printf("%s
    ",s);
           else
             printf("No Successor
    ");
       }
       return 0;
    }
  • 相关阅读:
    hdu 1686 Oulipo
    [NOI1997] 积木游戏
    错误录——未完待续
    NOI 2014 魔法森林
    hdu 4010 Query on The Trees
    求助大佬6——1种贪心
    51 nod 1205 流水线调度
    bzoj 1180: [CROATIAN2009]OTOCI
    HNOI2010 弹飞绵羊
    SDOI2008 洞穴勘测
  • 原文地址:https://www.cnblogs.com/372465774y/p/3603082.html
Copyright © 2011-2022 走看看