String Successor

D - String Successor
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu
Appoint description: 

Description

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<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
int t,n;
char s[105],str[105];
int main()
{
    cin>>t;
    while(t--)
    {
        int i,j,count=0;
        //cin>>s>>n;
        scanf("%s%d",s,&n);
        int cnt=0,len=strlen(s);
        for(i=0; i<len; i++)
        {
            str[i]=s[len-i-1];
            if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')||(s[i]>='0'&&s[i]<='9'))
            {
                count++;
                cnt=max(cnt,len-i-1);
            }
        }
        while(n--)
        {
            int count1=count,step=1;
            for(i=0; i<len; i++)
            {
                if(count&&step)
                {
                    if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')||(str[i]>='0'&&str[i]<='9'))
                    {
                        step=0;
                        char c=str[i];
                        if(c=='9')
                        {
                            str[i]='0';
                            step++;
                            if(len-1==i)
                            {
                                str[len]='1';
                                cnt=len++;
                                break;
                            }else if(cnt==i)
                            {
                                for(j=len;j>i+1;j--)str[j]=str[j-1];
                                str[cnt+1]='1';
                                cnt=i+1;
                                len++;
                                break;
                            }
                        }
                        else if(c=='Z')
                        {
                            str[i]='A';
                            step++;
                            if(len-1==i)
                            {
                                str[len]='A';
                                cnt=len++;
                                break;
                            }else if(cnt==i)
                            {
                                 for(j=len;j>i+1;j--)str[j]=str[j-1];
                                str[cnt+1]='A';
                                cnt=i+1;
                                len++;
                                break;
                            }
                        }
                        else if(c=='z')
                        {
                            str[i]='a';
                            step++;
                            if(len-1==i)
                            {
                                str[len]='a';cnt=len++;
                                break;
                            }else if(cnt==i)
                            {
                                for(j=len;j>i+1;j--)str[j]=str[j-1];
                                str[cnt+1]='a';
                                cnt=i+1;
                                len++;
                                break;
                            }
                        }
                        else
                        {
                            str[i]++;
                        }
                    }
                }
                else if(step)
                {
                    step=0;
                    str[i]++;
                    if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')||(str[i]>='0'&&str[i]<='9'))
                        count1++;
                }
                count=count1;
            }
            for(i=len-1; i>=0; i--)
                printf("%c",str[i]);
            printf("\n");
        }
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是Java代码实现: ```java import java.util.Random; public class BST { private Node root; private class Node { int val; Node left, right; Node(int val) { this.val = val; } } public void insert(int val) { root = insert(root, val); } private Node insert(Node node, int val) { if (node == null) { return new Node(val); } if (val < node.val) { node.left = insert(node.left, val); } else if (val > node.val) { node.right = insert(node.right, val); } return node; } public void delete(int val) { root = delete(root, val); } private Node delete(Node node, int val) { if (node == null) { return null; } if (val < node.val) { node.left = delete(node.left, val); } else if (val > node.val) { node.right = delete(node.right, val); } else { if (node.left == null && node.right == null) { node = null; } else if (node.left == null) { node = node.right; } else if (node.right == null) { node = node.left; } else { Node successor = findMin(node.right); node.val = successor.val; node.right = delete(node.right, successor.val); } } return node; } public boolean search(int val) { return search(root, val); } private boolean search(Node node, int val) { if (node == null) { return false; } if (node.val == val) { return true; } else if (val < node.val) { return search(node.left, val); } else { return search(node.right, val); } } public void update(int oldVal, int newVal) { delete(oldVal); insert(newVal); } public int successor(int val) { Node node = find(root, val); if (node == null) { return -1; } if (node.right != null) { return findMin(node.right).val; } else { Node successor = null; Node curr = root; while (curr != node) { if (node.val < curr.val) { successor = curr; curr = curr.left; } else { curr = curr.right; } } return successor == null ? -1 : successor.val; } } private Node find(Node node, int val) { if (node == null) { return null; } if (node.val == val) { return node; } else if (val < node.val) { return find(node.left, val); } else { return find(node.right, val); } } private Node findMin(Node node) { while (node.left != null) { node = node.left; } return node; } public static void main(String[] args) { BST bst = new BST(); Random rand = new Random(); int[] arr = new int[10]; System.out.print("原数组: "); for (int i = 0; i < 10; i++) { arr[i] = rand.nextInt(100); System.out.print(arr[i] + " "); bst.insert(arr[i]); } System.out.println(); System.out.println("插入后的BST: "); System.out.println(bst); int idx = rand.nextInt(10); int val = rand.nextInt(100); System.out.println("将第" + idx + "个数 " + arr[idx] + " 修改为 " + val); bst.update(arr[idx], val); arr[idx] = val; System.out.println(bst); idx = rand.nextInt(10); System.out.println("删除第" + idx + "个数 " + arr[idx]); bst.delete(arr[idx]); arr[idx] = -1; System.out.println(bst); idx = rand.nextInt(10); System.out.println("查找第" + idx + "个数 " + arr[idx] + " 是否存在: " + bst.search(arr[idx])); idx = rand.nextInt(10); int successor = bst.successor(arr[idx]); System.out.println("第" + idx + "个数 " + arr[idx] + " 的后继: " + successor); } @Override public String toString() { StringBuilder sb = new StringBuilder(); inOrder(root, sb); return sb.toString(); } private void inOrder(Node node, StringBuilder sb) { if (node == null) { return; } inOrder(node.left, sb); sb.append(node.val).append(" "); inOrder(node.right, sb); } } ``` 在 `main` 函数中,先生成一个长度为 10 的随机数组,并用 `insert` 方法将其中的数插入到 BST 中。然后,随机选取一个数,用 `update` 方法将其修改,再随机选取一个数,用 `delete` 方法将其删除。接着,随机选取一个数,用 `search` 方法查找其是否存在,最后随机选取一个数,用 `successor` 方法寻找其后继。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值