题解

120 条题解

  • 1
    @ 2021-08-29 16:51:50
    #include<bits/stdc++.h>
    using namespace std;
    
    string s;
    int n,x,y;
    int js(int k)
    {
        if(k==0)
            return ' ';
        char c=s[k];
        if(c+1>(char)96+(y-(n-1-k)))
        {
            char c=js(k-1);
            if(c==' ')
                return ' ';
            s[k]=c+1;
            return s[k];
        }
        else
        {
            s[k]=c+1;
            return s[k];
        }
    }
    int main()
    {
        scanf("%d%d%d\n",&x,&y,&n);
        getline(cin,s);
        for(int i=1; i<=5; i++)
        {
            char x=js(n-1);
            if(x==' ') break;
            cout<<s<<endl;
        }
    }
    
  • 1
    @ 2021-03-17 13:01:55
    #include <cmath>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <deque>
    using namespace std;
    
    namespace dts
    {
        int s,t,w;
        char c[26];
        
        void plus(int pos)
        {
            if (pos>=0)
            {
                if (c[pos]-'a'+1==t-(w-1-pos))
                {
                    plus(pos-1);
                    c[pos]=c[pos-1]+1;
                }
                else
                    c[pos]++;
            }
        }
        
        void main()
        {
            memset(c,0,sizeof(c));
            scanf("%d%d%d\n%s\n",&s,&t,&w,c);
            for (int i=1;i<=5&&c[0]-'a'+1<t-w+1;i++)
            {
                plus(w-1);
                printf("%s\n",c);
            }
        }
    }
    
    int main()
    {
        dts::main();
    }
    
  • 1
    @ 2017-08-22 23:55:04

    糊里糊涂的AC了

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    int main()
    {
        int  head1,back2,i,n,k,j,h;
        char head,back,a[50];
        scanf("%d %d %d",&head1,&back2,&n);
        getchar();
        back=back2+'a'-1;
        for(i=1;i<=n;i++)
        {
            scanf("%c",&a[i]);
        }
        for(k=1;k<=5;k++)
        {
            for(i=n;i>=1;i--)
            {
                if(a[i]+1<=back)
                {
                    if(a[i]+1<a[i+1]||i==n)
                    {
                        a[i]++;
                        for(j=i+1;j<=n;j++)
                            a[j]=a[j-1]+1;
                        for(j=1;j<=n;j++)
                            printf("%c",a[j]);
                        printf("\n");
                        break;
                    }
                }
            }
        } 
        return 0;
    }
    
  • 1
    @ 2017-07-11 19:51:15

    代码共17行。
    var
    s,t,w,i,j,k:longint;
    a,b:string;
    begin
    readln(s,t,w);
    readln(a);
    for i:=1 to 5 do
    for j:=w downto 1 do
    if ord(a[j])-96<(t-w+j) then begin//判断能不能继续
    b:=copy(a,1,j-1)+succ(a[j]);
    for k:=j+1 to w do b:=b+succ(b[k-1]);
    a:=b;
    writeln(a);
    b:='';
    break;
    end;
    end.
    succ真是个好东西。

  • 0
    @ 2017-05-14 10:24:57

    一个简单的递归,注意每一位什么时候该进位都不一样(比后一位小1)

    var
    time,s,t,w:longint;
    sta:string;
    procedure jia(x:longint);
    begin
        if ord(sta[x])-96=( t-( length(sta)-x ) ) then //判断是否该进位
        begin
            jia(x-1);
            sta[x]:=chr(ord(sta[x-1])+1);  //每当这一位要进位时,其结果都为前一位的下一个字母
        end
        else
        begin
            sta[x]:=chr(ord(sta[x])+1);
        end; 
    end;
    begin
        readln(s,t,w);
        readln(sta);
        time:=0;
        while ((ord(sta[1])-96)<t-w+1) and (time<5) do
        begin
            inc(time);
            jia(length(sta));
            writeln(sta);
        end; 
    end.
    
  • 0
    @ 2016-08-19 14:40:39

    这题和火星人十分相似
    每个jam数的获取策略如下:
    1 从右到左找到第一个“后继字母没出现在当前Jam数中”的字母”
    2 将这个字母更新为其后继字母
    3 从这个字母始后面的字母以步长为1逐渐递增
    这样就产生了一个新的Jam数。。
    下为标程...
    ```pascal
    program jam;

    var s,t,w,i,j,count:longint;
    st:string;
    temp,en:char;
    c:array['a'..'z']of boolean;

    begin
    readln(s,t,w);
    readln(st);

    count:=0;
    en:=chr(t+97);
    fillchar(c,sizeof(c),false);
    for i:=1 to w do c[st[i]]:=true;

    while (count<5) do
    begin
    for i:=w downto 1 do
    begin
    temp:=succ(st[i]);
    if (temp<en)and(c[temp]=false) then break;
    end;
    if (i=1)or(c[temp])then break;
    c[st[i]]:=false;
    c[temp]:=true;
    st[i]:=temp;
    for j:=i+1 to w do
    begin
    c[st[j]]:=false;
    st[j]:=succ(st[j-1]);
    c[st[j]]:=true;
    end;
    writeln(st);
    inc(count);
    end;
    end.
    ```

  • 0
    @ 2016-02-12 22:23:55

    var
    s,t,w,i,j,k:longint;
    a,b:string;
    begin
    readln(s,t,w);
    readln(a);
    for i:=1 to 5 do
    for j:=w downto 1 do
    if ord(a[j])-96<(t-w+j) then
    begin

    b:=copy(a,1,j-1)+succ(a[j]);
    for k:=j+1 to w do
    b:=b+succ(b[k-1]);
    a:=b;
    writeln(a);
    b:=' ';
    break;
    end;
    end.
    简短的标,O(∩_∩)O~
    讲解:
    首先,要先判断在第i位的字母是否是给出的表中的(即s到t之间的字母表)第i大,如果是则遍历前一位。知道不满足条件为止,然后从j+1到w位之间依次填上递增的字母,那么怎么判断呢?
    目前遍历到第i位字母,t-w+j即为字母表中第i大的字母的序号,ord(a[i])-96即为当前字母的位置,只要ord(a[i])-96>t-w+j就继续遍历否则处理,输出。话说succ真是好东西。

  • 0
    @ 2015-10-23 09:04:27

    program p1318;
    var s,t,w:integer;
    min,max:char;
    i:char;
    j:integer;
    str:string;
    flag:boolean;
    unuse:integer;
    used:array ['a'..'z'] of boolean;
    begin
    readln(s,t,w);
    min:=chr(s+96);
    max:=chr(t+96);
    for i:=min to max do used[i]:=false;
    readln(str);
    for j:=1 to w do used[str[j]]:=true;
    for j:=1 to 5 do
    begin
    flag:=false;
    unuse:=0;
    for i:=max downto min do
    begin
    if used[i] and flag then
    begin
    used[i]:=false;
    used[chr(ord(i)+1)]:=true;
    break;
    end;
    if used[i] then inc(unuse);
    if (not used[i]) then flag:=true;
    used[i]:=false;
    end;
    if (unuse=w) then break;
    inc(i);
    while unuse>0 do
    begin
    inc(i);
    used[i]:=true;
    dec(unuse);
    end;
    for i:=min to max do if used[i] then write(i);
    writeln;
    end;
    end.

  • 0
    @ 2015-09-17 20:58:59

    <code>
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    int i,j,w,k;
    char l,r,ch[26];
    int main()
    {
    cin>>i>>j>>w;
    l=i+'a'-1;
    r=j+'a'-1;
    cin>>ch;
    for(int i=1;i<=5;i++)
    {
    j=w-1;
    while(j>=0 && ch[j]==r+j+1-w)j--;
    if(j==-1 )break;
    ch[j]++;
    for(k=j+1;k<=w-1;k++){ch[k]=ch[k-1]+1;}
    cout<<ch<<endl;
    }
    return 0;
    }
    </code>

  • 0
    @ 2015-07-13 15:58:25

    强行搜索,递归形式,可能写的比较乱大家凑和看吧
    #include<iostream>
    using namespace std;
    char k[27];
    int s, t, w, tmp = 0;
    char get(int i)
    {
    if(i < 0)
    return 'A';
    if(k[i] - 'a' + 1 == t - (w - 1 - i))
    {
    char p = get(i - 1);
    if(p == 'A')
    return 'A';
    k[i] = p + 1;
    tmp++;
    return char(p + 1);
    }
    else
    return k[i] = char(k[i] + 1);
    }
    bool dfs()
    {
    int i = w - 1;
    char p = get(i);
    if(p == 'A')
    return false;
    k[i] = p;
    i -= tmp, tmp = 0;
    return true;
    }
    int main()
    {
    cin >> s >> t >> w;
    cin >> k;
    for(int p = 0; p < 5; p++)
    {
    if(dfs())
    cout << k << endl;
    else
    break;
    }
    system("pause");
    return 0;
    }

  • 0
    @ 2015-06-02 21:56:21

    一开始真是自己把自己搞晕了,以前有点pascal经验,看了别人的题解,去网上查了几个函数功能,大概就明白了整个的意思

    对于每个位置上的字母,从大位置到小位置去判断,如果它不是当前位置上应该有的最大值(比如,w=5,t=10,那第五位上应有的最大值是10代表的j,第四位上应有的最大值i),则让它加1,并让从它开始每个后面的值等于前一个+1(如bdfij,找到f,f+1变成g,下一位变成g+1就是h,再下一位变成h+1就是i,整个变成bdghi),然后输出。

    我没想到的这一步,就是再按这个规则不断重新来(一定要从头来过),一共进行5遍此规则即可。记得用一次规则完毕就要break掉,不然会输出很多很多答案。

    #include<stdio.h>
    int main( )
    {
    int s,t,w,i,max,l=0,j;
    char a[30];

    scanf("%d %d %d\n",&s,&t,&w);

    max=t+96;

    for(i=1;i<=w;i++)
    scanf("%c",&a[i]);

    for(l=1;l<=5;l++)
    {
    for(i=w;i>=1;i--)
    {
    if(a[i]!=max-(w-i))
    {
    a[i]++;

    for(j=i+1;j<=w;j++)
    a[j]=a[j-1]+1;

    for(j=1;j<=w;j++)
    printf("%c",a[j]);

    printf("\n");
    break;
    }
    }
    }

    return 0;

    }

    • @ 2015-06-02 21:57:36

      整个过程没有递归那么简洁,但是便于理解。

  • 0
    @ 2014-10-29 16:51:58

    var
    s,t,w,i,j,k:integer;
    a,b:string;
    begin
    readln(s,t,w);
    readln(a);
    for i:=1 to 5 do
    for j:=w downto 1 do
    if ord(a[j])-96<(t-w+j) then
    begin
    b:=copy(a,1,j-1)+succ(a[j]);
    for k:=j+1 to w do
    b:=b+succ(b[k-1]);
    a:=b;
    writeln(a);
    b:=' ';
    break;
    end;
    end.

  • 0
    @ 2013-11-06 00:33:08

    评测结果

    编译成功

    foo.cpp: In function 'int main()':
    foo.cpp:26:28: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'char (*)[30]' [-Wformat]
    foo.cpp:33:58: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    foo.cpp:37:54: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    foo.cpp:42:58: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

    测试数据 #0: Accepted, time = 15 ms, mem = 464 KiB, score = 10

    测试数据 #1: Accepted, time = 0 ms, mem = 468 KiB, score = 10

    测试数据 #2: Accepted, time = 0 ms, mem = 468 KiB, score = 10

    测试数据 #3: Accepted, time = 11 ms, mem = 468 KiB, score = 10

    测试数据 #4: Accepted, time = 0 ms, mem = 460 KiB, score = 10

    测试数据 #5: Accepted, time = 0 ms, mem = 464 KiB, score = 10

    测试数据 #6: Accepted, time = 0 ms, mem = 468 KiB, score = 10

    测试数据 #7: Accepted, time = 0 ms, mem = 472 KiB, score = 10

    测试数据 #8: Accepted, time = 0 ms, mem = 464 KiB, score = 10

    测试数据 #9: Accepted, time = 0 ms, mem = 460 KiB, score = 10

    Accepted, time = 26 ms, mem = 472 KiB, score = 100
    代码

    #include <stdio.h>
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <math.h>
    #include <cstdlib>

    using namespace std;

    int s , t , w;
    char str[30];
    char a[30];
    int i , j;
    char last;
    int num;
    int flag;

    int main()
    {
    while( scanf( "%d %d %d", &s , &t , &w ) != EOF )
    {
    num = 5;
    flag = 0;
    memset( str , 0 , sizeof( str ) );
    memset( a , 0 , sizeof( a ) );
    scanf( "%s" , &str );
    for( i = 0 ; i < t - s + 1 ; i++ )
    a[i] = 'a' + s + i - 1;
    last = a[t - s];
    while( num )
    {
    for( i = strlen( str ) - 1 ; i >= 0 ; i-- )
    if( str[i] != last - strlen( str ) + i + 1 )
    {
    num--;
    str[i] += 1;
    for( j = i + 1 ; j < strlen( str ) ; j++ )
    str[j] = str[i] + j - i;
    break;
    }
    for( i = strlen( str ) - 1 ; i >= 0 ; i-- )
    if( str[i] != last - strlen( str ) + i + 1 )
    flag = 1;
    if( flag == 0 )
    break;
    else
    flag = 0;
    cout << str << endl;
    }
    }
    return 0;
    }

  • 0
    @ 2013-11-03 21:17:28

    program xx;
    var
    min,max,wei,i,j,t:longint;
    str1:string;
    a:array[0..25]of longint;
    b:array[1..25]of longint;
    c:array[1..26]of char;
    begin
    readln(min,max,wei);
    readln(str1);
    for i:=1 to wei do
    begin
    b[i]:=ord(upcase(str1[i]))-ord('A')+1;
    end;
    a[0]:=0;
    for i:=1 to wei do a[i]:=b[i];
    t:=0;
    while(a[0]=0)do
    begin
    j:=wei;
    while(a[j]=max-wei+j)do dec(j);
    a[j]:=a[j]+1;
    for i:=j+1 to wei do a[i]:=a[i-1]+1;
    for i:=1 to wei do write(char((a[i]+ord('a'))-1));
    t:=t+1;
    writeln;
    if(t=5)then break;
    end;
    end.

  • 0
    @ 2013-10-18 20:12:39

    有点厉害

  • 0
    @ 2012-11-09 10:58:35

    没使用递归就做出来了,这道题轻微变态的说。。。。。

    点这里查看程序源码+详细题解

  • 0
    @ 2012-10-07 16:38:02

    完全没看懂的某人在拜读这一页的全部大牛留言后来详细解释一下题意……

    首先,题目中有这样一句话,“从2到10,表示只能使用{b,c,d,e,f,g,h,i,j}这些字母”。那么,b

  • 0
    @ 2012-08-09 10:40:49

    ac 50..为何是如此水题。。

  • 0
    @ 2012-08-07 15:28:03

    ac 40 留念

  • 0
    @ 2010-07-14 13:21:58

    我自觉很漂亮的程序..

    #include

    using namespace std;

    int s,t,w,i,a[26],j;

    char te;

    bool failed;

    void inc(int l,int limit)

    {

    if(a[l]

信息

ID
1318
难度
1
分类
组合数学 点击显示
标签
递交数
2115
已通过
1438
通过率
68%
被复制
23
上传者