ID Codes UVA 146(求字典序比当前字符串小的最大字符串)

说说:

题意其实很简单,就是给你一个由小写英文字母组成的字符串,然后让你求字典序比当前字符串小的最大的字符串。解法的话,就是从字符串的末尾开始遍历,若得到的子串已经是该字串所能得到的最小字典序,则继续往前遍历。否则,先在子串中,找到比原字串的首字符小的最大字符,将两者交换位置。然后将除首字符以外的其他字串排列获取最大字典序的子串即可。具体方案,看源代码好了。

源代码:

#include <stdio.h>
#include <string.h>
#define MAX 50+5

char ID[MAX];

int ismin(char*);//判断当前子串是否为最小字典序
void getans(char*);//获取比当前字符串小的最大字符串

int main(){
  int i,len,offset,NO;
  char* p;
//  freopen("data","r",stdin);
  while(scanf("%s",ID)){
    if(ID[0]=='#')
      break;

    len=strlen(ID);
    NO=1;
    for(i=2;i<=len;i++){//从字符串末尾开始遍历
     p=&ID[len-i];
     if(!ismin(p)){
       getans(p);
       NO=0;
       break;
     }
    }

    if(NO)
      printf("No Successor\n");
    else
      printf("%s\n",ID);
  }

  return 0;
}

int ismin(char* p){
  int i,len;

  len=strlen(p);

  for(i=1;i<len;i++)
    if(p[i]>p[i-1])
      return 0;

  return 1;
}

void getans(char* p){
  int i,len,pos,j;
  char temp;

  len=strlen(p);

  for(i=1;i<len;i++)//首先得到比当前首字符小的字符位置
    if(p[i]>p[0]){
     pos=i;
     break;
    }

  for(i++;i<len;i++)
    if(p[i]<p[pos]&&p[i]>p[0])//获取比当前首字符小的最大字符的位置
      pos=i;

  temp=p[0];
  p[0]=p[pos];
  p[pos]=temp;

  p++;
  len--;

  for(i=1;i<len;i++)//将除首字符以外的子串排列获得最大字典序
    for(j=0;j<len-i;j++)
      if(p[j]>p[j+1]){
        temp=p[j];
	p[j]=p[j+1];
	p[j+1]=temp;
      }

  return ;
}
时间: 2024-12-12 12:26:49

ID Codes UVA 146(求字典序比当前字符串小的最大字符串)的相关文章

求一个包加减乘除和小括号的字符串的结果

解题思路和代码原型来源于牛课网, 我自己实现了一遍,添加了部分注释 1 package problems_2016_08_10; 2 3 import java.util.LinkedList; 4 /* 5 总述: 6 一共有五个函数分别是: 7 8 getvalue(): 9 参数:待求的字符串. 10 作用:一个启动函数,调用value函数 11 返回值:返回value函数返回值的第一个参数,就是求出的值 12 13 value(): 14 参数:总的待求的字符串,从哪一个位置开始求 15

UVA - 146 - ID Codes (枚举排列)

UVA - 146 ID Codes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In order to exercise greater control over its ci

Brute Force &amp; STL --- UVA 146 ID Codes

 ID Codes  Problem's Link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=3&problem=82&mosmsg=Submission+received+with+ID+14418598 Mean: 求出可重排列的下一个排列. analyse: 直接用STL来实现就可.自己手动写了一个,并不复杂.

UVA 146 ID Codes(下一个排列)

C - ID Codes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Appoint description:  System Crawler  (2014-05-12) Description  ID Codes  It is 2084 and the year of Big Brother has finally arrived, albeit a century l

UVA 146 ID code(next_permutation的运用)

ID code It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In order to exercise greater control over its citizens and thereby to counter a chronic breakdown in law and order, the Government decides on a radical measure

POJ 1146:ID Codes

ID Codes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6281 Accepted: 3769 Description It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In order to exercise greater control over its citizens and thereby

寒假集训.ID Codes

ID Codes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description  ID Codes  It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In order to exercise greater control over its citi

UVA - 146

 ID Codes  It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In order to exercise greater control over its citizens and thereby to counter a chronic breakdown in law and order, the Government decides on a radical meas

Uva 796 求桥

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=737 /*** Uva 796 求桥 题目要求:输出题目中所有的桥,按其所连接的点从小到大的顺序输出 解题思路:tarjan算法,所有树枝边都是桥(dfn[u]<low[v]),利用vector存储一下就可以了 */ #include <stdio.h> #include &