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;
}
原文地址:https://www.cnblogs.com/372465774y/p/3603082.html