zoukankan      html  css  js  c++  java
  • 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 measure--all citizens are to have a tiny microcomputer surgically implanted in their left wrists. This computer will contains all sorts of personal information as well as a transmitter which will allow people's movements to be logged and monitored by a central computer. (A desirable side effect of this process is that it will shorten the dole queue for plastic surgeons.)
    
    An essential component of each computer will be a unique identification code, consisting of up to 50 characters drawn from the 26 lower case letters. The set of characters for any given code is chosen somewhat haphazardly. The complicated way in which the code is imprinted into the chip makes it much easier for the manufacturer to produce codes which are rearrangements of other codes than to produce new codes with a different selection of letters. Thus, once a set of letters has been chosen all possible codes derivable from it are used before changing the set.
    
    For example, suppose it is decided that a code will contain exactly 3 occurrences of `a', 2 of `b' and 1 of `c', then three of the allowable 60 codes under these conditions are:
    
          abaabc
          abaacb
          ababac
    These three codes are listed from top to bottom in alphabetic order. Among all codes generated with this set of characters, these codes appear consecutively in this order.
    
    Write a program to assist in the issuing of these identification codes. Your program will accept a sequence of no more than 50 lower case letters (which may contain repeated characters) and print the successor code if one exists or the message `No Successor' if the given code is the last in the sequence for that set of characters.
    
    Input and Output
    
    Input will consist of a series of lines each containing a string representing a code. The entire file will be terminated by a line consisting of a single #.
    
    Output will consist of one line for each code read containing the successor code or the words `No Successor'.
    
    Sample input
    
    abaacb
    cbbaa
    #
    Sample output
    
    ababac
    No Successor
    
    发现其实next_permutation可以给字符排序
    */
    #include <iostream>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<cmath>
    #include<string.h>
    #include<stdio.h>
    #include<stdlib.h>
    using namespace std;
    #define maxn 100
    char a[maxn];
    int v[maxn];
    char s[maxn];
    int main()
    {
        int ok,i;
        while(scanf("%s",s),strcmp(s,"#"))
        {
            bool bb=false;
            //printf("%d
    ",strlen(s));
            ok=0;
            for(i=0;i<strlen(s);i++)
                v[i]=(int)(s[i]-'a');
                //sort(v,v+strlen(s));
            do
            {
                for(i=0;i<strlen(s);i++)
                a[i]=(char)(v[i]+'a');
                a[i]='';//忘记加反斜杠了
                //printf("---%s----
    ",a);
                if(ok==1)
                {
                    bb=true;//开始用ok判断,发现当是最后一个的时候ok也会是1
                    /*for(i=0;i<strlen(s);i++)
                    printf("%c",a[i]);
                    printf("
    ");*/
                    printf("%s
    ",a);
                    break;
                }
                if(strncmp(a,s,strlen(s))==0)
                {
                    ok=1;
                }
            }while(next_permutation(v,v+strlen(s)));
            //printf("%d
    ",ok);
            if(!bb)
                printf("No Successor
    ");
        }
        return 0;
    }
    /*还可以如此简单*/
    #include <iostream>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<cmath>
    #include<string.h>
    #include<stdio.h>
    #include<stdlib.h>
    using namespace std;
    #define maxn 100
    char s[maxn];
    int main()
    {
        int i;
        while(scanf("%s",s),strcmp(s,"#"))
        {
            i=0;
            do
            {
                if(i==1)
                {
                    i=-1;
                    printf("%s
    ",s);
                    break;
                }
                i++;
            }
            while(next_permutation(s,s+strlen(s)));
            if(i!=-1)
                printf("No Successor
    ");
        }
        return 0;
    }
  • 相关阅读:
    KMP算法
    cocos2d-x jsbinding 在线更新策略设计
    AS3动画效果公式,常用处理公式代码,基本运动公式,三角公式
    理解引导行为:路径跟踪
    适用于任何语言的编程策略
    Using中return对象
    js计算两个时间相差天数
    fastReport 绑定DataBand数据源后还是打印出一条数据
    无法处理文件 MainForm.resx,因为它位于 Internet 或受限区域中,或者文件上具有 Web 标记。要想处理这些文件,请删除 Web 标记
    附加数据库后登陆报错
  • 原文地址:https://www.cnblogs.com/heqinghui/p/3209881.html
Copyright © 2011-2022 走看看