c语言解决the next permutation 问题

问题描述就直接用现成的了,主要说下我的设计思路和算法

Description

 
For this problem, you will write a program that takes a (possibly long) string of decimal digits, and
outputs the permutation of those decimal digits that has the next larger value (as a decimal number)
than the input number.  For example:
 
123 -> 132
279134399742 -> 279134423799
 
It is possible that no permutation of the input digits has a larger value.  For example, 987.

Input

 
The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that
follow.  Each data set is a single line that contains the data set number, followed by a space, followed
by up to 80 decimal digits which is the input value.
 

Output

 
For each data set there is one line of output.  If there is no larger permutation of the input digits, the
output should be the data set number followed by a single space, followed by the string BIGGEST.  If
there is a solution, the output should be the data set number, a single space and the next larger
permutation of the input digits.
 
 

Sample Input

3
1 123
2 279134399742
3 987

Sample Output

1 132
2 279134423799 
3 BIGGEST 

以上的问题的描述,这是一道ACM的竞赛题

开始的思路是用int来处理,但是发现长度不够,用字符串来处理了

 

设计思路:

从最后出发,看是否是非递减序列,一直到非递减的第一个字符,记为change,把前非递减序列保存在一个数组gg1[]中,如2345667543321 中 我们从1出发一直到7都是非递减的,gg1[]中为gg1[]=“1233457”,change=6;同时把剩余的序列保存在gg2[]中

gg2[]=“23456”(正序),在gg1[]中搜索找到比change大第一个数 在gg1[]="1233457"中这个数为7,change和这个数交换,现在gg1[]=1233456 change=7;把change插到gg2中的最后位置gg2=“234567” gg3=gg2+gg1;gg3就是最小的大于原数的数

一下是几个例子:gg为原数组,其他定义不变:

1:gg=123345698765

gg1=56789

change = 6

交换后:

change与7交换:

gg1=56689

 

gg2=123345

gg2=gg2+change = 1233457

gg3 = gg2+gg1=123345756689

2:

gg = 45678

gg1=8

change = 7

gg1交换后

gg1=7

change=8

gg2=4568

gg=gg2+gg1=45687

3:

gg=987654321

这时gg1=123456789而且gg1,gg2长度相等,说明gg为最大数:输出:BIGGEST

 

代码如下:

(点点取巧,没把gg1和gg2连接起来,直接在输出的地方控制了一下):

 

 

#include <iostream>
#include <string>
#include<string.h>
using namespace std;
int main()
{   int i;
string name[1000];//用于保存输入的字符串组,每组都单独处理
 int a[1000];//用于保存组数的序号
 char gg[1000]; //文章中的gg
 char gg1[1000];//文章中的gg1

 string result[1000];
   
     int t = 0;
  cin >> t;
    for (i = 0; i < t; ++i)
    {
  cin >> a[i];
        cin >> name[i];
  
    }
 for(i = 0;i<t;++i){
  strcpy(gg,name[i].c_str());
  int flag = 1;//标记找到了change,这里的change用hh代替了 ,因为我程序和文章不是同一时间写的
  int flag1 =1;//表示输入为最大
  char hh;
  char wo;
  int len = strlen(gg);
  if(len<=1) flag1 =0;//只有一位时默认为最大
  gg1[0] = gg[len-1];
   int temp = len-2;
   int fus = 0;
  while(flag&&(temp+1)){
   if(gg1[fus]<=gg[temp]){
      gg1[fus+1]=gg[temp];
      temp--;
      fus++;
      hh = gg[temp];
   }else{
      hh = gg[temp];
      flag = 0;
   }
   if(temp+1 ==0){
    flag1 = 0; //当gg和gg1长度相等时,说明输入的序列是最大的  
   }
   gg1[fus+1] = '\0';

  }
  if(flag1){//输入不是最大时按上文中处理
   for(int j= 0;j<=fus;j++){
   if(hh<=gg1[fus]){
    wo = hh;
    hh = gg1[fus];
    gg1[fus]= wo;
   }
  }
  gg[temp] =hh;//这
  gg[temp+1]='\0';// 这两行代码相当于gg2保存到了gg中
  printf("%d %s%s\n",a[i],gg,gg1);//gg gg1一起输入,其实输出的是文中的gg3
  
  }else{
   printf("%d BIGGEST\n",a[i]);//最大是直接输出BIGGEST;
  }
  
 
 }
    return 0;
}

 

验证

输入:

5
1 3243256436547858954324324
2 4
3 45
4 76543321
5 3456436576547568687987879870870543523421354375687687685

输出:

1 3243256436547858954324342
2 BIGGEST
3 54
4 BIGGEST
5 3456436576547568687987879870870543523421354375687687856

输入:

3
1 0998766123
2 000000000
3 11

输出:

1 0998766132
2 BIGGEST
3 BIGGEST

 

 

程序初步验证没有问题,如有读者发现漏洞,请指正!

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值