String Successor(模拟)

本文针对一个特定的字符串操作问题提供了解决方案。该问题要求进行多次操作,改变字符串中字符的ASCII值,处理数字和字母的进位问题,并实现相应的算法。文章详细介绍了通过C++实现的解题思路及代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3490

题意:给出一个字符串,一个操作次数,每次操作从当前字符串最右边的字符开始,如果字符串没有数字和字母则每次使其当前字符ASCII+1,否则,如果当前字符为z(Z),则其加1后为a(A ),并向前进位1给左边的最接近自己的字符或数字,如果当前字符为9,则加1后为0,并),并向前进位1给左边的最接近自己的字符或数字。如果某个数字或字符为最左边的数字或字符,并向前进位1,则在该字符或数字左边增加一个与其相同的字符或数字。

思路:理解题意并注意进位就可以了。。

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <algorithm>
 6 using namespace std;
 7 int isdigit(char ch)
 8 {
 9     if (ch >= '0'&& ch <= '9')
10         return 1;
11     return 0;
12 }
13 int isalpha(char ch)
14 {
15     if (ch >= 'A'&&ch <= 'Z')
16         return 1;
17     else if (ch >= 'a'&&ch <= 'z')
18         return 2;
19     else
20         return 0;
21 }
22 void judge(string &s,int i)
23 {
24     char ch;
25     if (s[i]=='Z'||s[i]=='z'||s[i]=='9')
26     {
27         if (s[i]=='Z') {ch = 'A';s[i] = 'A';}
28         if (s[i]=='z') {ch = 'a';s[i] = 'a';}
29         if (s[i]=='9') {ch = '1';s[i] = '0';}
30     }
31     else
32     {
33         s[i]++;
34         return ;
35     }
36     int j;
37     for (j = i-1; j >= 0; j--)
38     {
39         if (isdigit(s[j])||isalpha(s[j]))
40         break;
41     }
42     string :: iterator it = s.begin();
43     if (j < 0)
44         s.insert(it+i,ch);
45         else
46          judge(s,j);
47 }
48 int main()
49 {
50     int t,k;
51     scanf("%d%*c",&t);
52     string s;
53     while(t--)
54     {
55         cin>>s;
56         scanf("%d",&k);
57         while(k--)
58         {
59             int len = s.size();
60             int flag = 0,i;
61             for (i = len-1; i >= 0; i--)
62             {
63                 if (isdigit(s[i])||isalpha(s[i]))
64                 {
65                     flag = 1;
66                     break;
67                 }
68             }
69             if(!flag)
70             {
71                 s[len-1]+=1;
72                 cout<<s<<endl;
73                 continue;
74             }
75             judge(s,i);
76             cout<<s<<endl;
77         }
78         puts("");
79     }
80     return 0;
81 }
View Code

 

转载于:https://www.cnblogs.com/lahblogs/p/3599877.html

### Successor 的定义与用法 在编程领域,`successor` 通常指代某个对象、节点或状态的“后继者”。具体含义和用法取决于上下文环境。以下是 `successor` 在不同场景中的定义与用法: #### 1. 数据结构中的 Successor 在数据结构中,`successor` 常用于描述一个节点的直接后继节点。例如,在二叉搜索树(Binary Search Tree, BST)中,某个节点的 successor 是其右子树中最左侧的节点[^1]。如果该节点没有右子树,则需要沿着父节点路径向上查找,直到找到第一个值大于当前节点的祖先节点。 ```python def find_successor(node): if node is None: return None # 如果有右子树,successor 是右子树中最左节点 if node.right: current = node.right while current.left: current = current.left return current # 如果没有右子树,则沿父节点路径向上查找 ancestor = node.parent child = node while ancestor and child == ancestor.right: child = ancestor ancestor = ancestor.parent return ancestor ``` #### 2. 责任链模式中的 Successor 在责任链设计模式中,`successor` 表示处理请求的下一个对象。每个对象都有一个指向其后继者的引用,并将请求沿着链传递给下一个处理器,直到某个处理器能够处理该请求为止[^3]。 ```java abstract class Handler { protected Handler successor; public void setSuccessor(Handler successor) { this.successor = successor; } public abstract void handleRequest(int request); } class ConcreteHandlerA extends Handler { @Override public void handleRequest(int request) { if (request >= 0 && request < 10) { System.out.println("ConcreteHandlerA handled request " + request); } else if (successor != null) { successor.handleRequest(request); } } } class ConcreteHandlerB extends Handler { @Override public void handleRequest(int request) { if (request >= 10 && request < 20) { System.out.println("ConcreteHandlerB handled request " + request); } else if (successor != null) { successor.handleRequest(request); } } } ``` #### 3. 接口实现中的 Successor 在接口设计中,`successor` 可以表示一种逻辑上的继承关系或行为扩展。通过实现接口,类可以定义自己的行为,同时遵循接口的规范[^2]。虽然接口本身不涉及 `successor` 的具体定义,但在某些框架中,接口可能被用来描述某种顺序或链条关系。 ```java interface ChainElement { void setNext(ChainElement successor); void process(Request request); } class Authentication implements ChainElement { private ChainElement successor; @Override public void setNext(ChainElement successor) { this.successor = successor; } @Override public void process(Request request) { if (request.isAuthenticated()) { System.out.println("User is authenticated."); if (successor != null) { successor.process(request); } } else { System.out.println("Authentication failed."); } } } ``` ### 总结 `Successor` 的定义和用法因上下文而异。在数据结构中,它通常指代节点的后继;在责任链模式中,它是请求传递链中的下一个处理器;在接口实现中,它可以表示逻辑上的继承或行为扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值