标题:浙大oj3490 String Successor 求助~~
只看楼主
LeslieCh
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2013-11-27
结帖率:77.78%
已结贴  问题点数:15 回复次数:6 
浙大oj3490 String Successor 求助~~
String Successor

--------------------------------------------------------------------------------

Time Limit: 2 Seconds      Memory Limit: 65536 KB

--------------------------------------------------------------------------------

The successor to a string can be calculated by applying the following rules:

Ignore the nonalphanumerics unless there are no alphanumerics, in this case, increase the rightmost character in the string.
The increment starts from the rightmost alphanumeric.
Increase a digit always results in another digit ('0' -> '1', '1' -> '2' ... '9' -> '0').
Increase a upper case always results in another upper case ('A' -> 'B', 'B' -> 'C' ... 'Z' -> 'A').
Increase a lower case always results in another lower case ('a' -> 'b', 'b' -> 'c' ... 'z' -> 'a').
If the increment generates a carry, the alphanumeric to the left of it is increased.
Add an additional alphanumeric to the left of the leftmost alphanumeric if necessary, the added alphanumeric is always of the same type with the leftmost alphanumeric ('1' for digit, 'A' for upper case and 'a' for lower case).

Input
There are multiple test cases. The first line of input is an integer T ≈ 10000 indicating the number of test cases.

Each test case contains a nonempty string s and an integer 1 ≤ n ≤ 100. The string s consists of no more than 100 characters whose ASCII values range from 33('!') to 122('z').

Output
For each test case, output the next n successors to the given string s in separate lines. Output a blank line after each test case.

Sample Input
4
:-( 1
cirno=8 2
X 3
/**********/ 4

Sample Output
:-)

cirno=9
cirnp=0

Y
Z
AA

/**********0
/**********1
/**********2
/**********3



代码:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<iostream>
using namespace std;
char f(char x)
{
    if(x=='9'){return '0';}
    if(x=='z'){return 'a';}
    if(x=='Z'){return 'A';}
    else
    {return 'n';}
}
int main()
{
    int T,i,n,j,k,flag,q;
    char s[105],c[105];
    cin>>T;
    for(i=0;i<T;i++)
    {
        memset(c,0,sizeof(c));
        scanf("%s%d",s,&n);
        for(q=0;q<n;q++)
        {     
        k=0;flag=0;
        for(j=strlen(s)-1;j>=0;j--)
        {
            if(isalnum(s[j]))
            {flag=1;}
            c[k]=s[j];
            k++;
        }
        if(flag==1)
        {
            for(j=0;j<strlen(s);j++)
            {
                if(isalnum(c[j])){break;}
            }
            if(f(c[j])!='n')//jin wei
            {
                c[j]=f(c[j]);
                j++;
                while(j<strlen(s))
                {
                    if(isalnum(c[j]))
                    {
                        if(f(c[j])!='n')
                        {c[j]=f(c[j]);}
                        else
                     {c[j]++;break;}
                    }
                    j++;
                }
                if(j>=strlen(s))
                {
                    for(j=strlen(s)-1;j>=0;j--)
                    {
                        if(isalnum(c[j])){break;}
                    }
                    for(k=j+1;k<=strlen(s)-1;k++)
                    {
                        c[k+1]=c[k];
                    }
                    c[k+1]='\0';
                    if(isdigit(c[j]))
                    {c[j+1]='1';}
                    else
                    {c[j+1]=c[j];}
                }
            }
            else
            {c[j]++;}
        }
        else
        {c[0]++;}
        k=0;
        for(j=strlen(c)-1;j>=0;j--)
        {
            s[k]=c[j];
            k++;
            printf("%c",c[j]);
        }
        printf("\n");
        }
    }
}


试了好多数据都找不出错的,提交上去就是不对。。。求指点~!        
                    
搜索更多相关主题的帖子: character following increase Memory unless 
2014-03-28 16:38
LeslieCh
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2013-11-27
得分:0 
.
2014-03-28 16:39
LeslieCh
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2013-11-27
得分:0 
2014-03-30 12:56
yaojiali920
Rank: 2
等 级:论坛游民
帖 子:33
专家分:14
注 册:2014-2-18
得分:5 
顶上去,虽然我看不懂
2014-03-30 20:05
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
得分:5 
就不看你的代码了,这么多if else套在一起实在眼花。另外,你这C和C++杂在一起写看起来也很混乱(其实C++你也就开始cin>>T这句用了一下)。

送一段代码,有兴趣可以提交一下看看能过不。

程序代码:
#include <stdio.h>
#include <string.h>
#include <ctype.h>

char * incstr(char * s)
{
    int n, f, c, i;

    for(i = n = strlen(s) - 1; i >= 0 && !isalnum(s[i]); i--);

    if(i < 0){ s[n]++; return s;}

    for(f = 1; i >= 0 && f; i--)
        if(isalnum(s[i]))
        switch(s[i])
        {
            case '9': s[i] = '0'; f = 1; c = '1'; break;
            case 'Z': s[i] = 'A'; f = 1; c = 'A'; break;
            case 'z': s[i] = 'a'; f = 1; c = 'a'; break;
            default: s[i]++; f = 0;
        }
    if(f)
    {
        for(i = n + 2; i--; s[i + 1] = s[i]);
        s[0] = c;
    }
    return s;
}

int main()
{
    int t, n;
    char s[128];
    for(scanf("%d", &t); t--; puts(""))
    for(scanf("%s%d", s, &n); n--; puts(incstr(s)));
    return 0;
}

重剑无锋,大巧不工
2014-03-30 21:59
wp231957
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:神界
等 级:版主
威 望:422
帖 子:13681
专家分:53296
注 册:2012-10-18
得分:5 
看样子 也就是
#include <ctype.h>


isalnum  是一伙的

DO IT YOURSELF !
2014-03-30 22:03
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
得分:0 
早上好。刚刚去浙大OJ去提交了一下,上面的代码也是WA。

于是重新阅读了一遍题目,问题出在对“Add an additional alphanumeric to the left of the leftmost alphanumeric if necessary”这句话的理解上。

之前我没注意这句话,关于进位是直接插在最左边的字符之前。而这句话的意思是将进位插在最左边的字母数字之前。

下面是修改后的AC代码,有兴趣请对比两段代码。

程序代码:
#include <stdio.h>
#include <string.h>
#include <ctype.h>

char * incstr(char * s)
{
    int n, f, c, i, j;

    for(i = n = strlen(s) - 1; i >= 0 && !isalnum(s[i]); i--);

    if(i < 0){ s[n]++; return s;}

    for(f = 1; i >= 0 && f; i--)
        if(isalnum(s[i]))
        switch(s[i])
        {
            case '9': s[i] = '0'; f = 1; c = '1'; j = i; break;
            case 'Z': s[i] = 'A'; f = 1; c = 'A'; j = i; break;
            case 'z': s[i] = 'a'; f = 1; c = 'a'; j = i; break;
            default: s[i]++; f = 0;
        }
    if(f)
    {
        for(i = n + 2; i-- > j; s[i + 1] = s[i]);
        s[j] = c;
    }
    return s;
}

int main()
{
    int t, n;
    char s[128];
    for(scanf("%d", &t); t--; puts(""))
    for(scanf("%s%d", s, &n); n--; puts(incstr(s)));
    return 0;
}

重剑无锋,大巧不工
2014-03-31 07:53



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-429295-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.422873 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved