Link: https://ac.nowcoder.com/acm/problem/16644
Source: Nowcoder
#include<bits/stdc++.h>
using namespace std;
string solve(char b,char e,int p1,int p2,int p3)
{
if(e-b==1)return "";
if(!(isalpha(b)&&isalpha(e)&&b<e||isdigit(b)&&isdigit(e)&&b<e))
return "-";
string ans="";
for(char i=b+1;i<e;i++)
{
for(int j=1;j<=p2;j++)
{
ans+=i;
}
}
if(p1==2&&isalpha(b))
{
for(int i=0;i<ans.length();i++)
{
ans[i]=ans[i]-'a'+'A';
}
}
if(p1==3)
{
for(int i=0;i<ans.length();i++)
ans[i]='*';
}
if(p3==2){
reverse(ans.begin(),ans.end());
}
return ans;
}
int main()
{
int p1,p2,p3;
cin>>p1>>p2>>p3;
string a;
cin>>a;
string ans="";
for(int i=0;i<a.length();i++)
{
if(a[i]=='-'&&i-1>=0&&i+1<a.length())
{
ans+=solve(a[i-1],a[i+1],p1,p2,p3);
}
else
{
ans+=a[i];
}
}
cout<<ans<<endl;
return 0;
}
- Find that the operation of expanding the string occurs repeatedly when encountering '-'. Then consider encapsulating a function.
- Traverse the string. My method is to move it backwards, but this is obviously cumbersome and the complexity increases when calculating the interval length. The best way is to create another string 'ans' to store the answer.
- If 'a[i]' is not '-', add it directly to 'ans'.
- If it is '-', add the expanded string to 'ans'.
- How to expand? First exclude illegal cases:
- The first illegal case is "a-b" -> "ab", "2-3" -> "23", in this case, regardless of whether it is a letter or a number, it can be understood as: the character after the '-' is 1 greater than the character before the '-'. That is, 'e-b == 1'. In this case, return an empty string "" (where 'e' represents the character before '-','b' represents the character after '-').
- "Both sides of the minus sign are lowercase letters or numbers, and according to the order of ASCII codes, the character to the right of the minus sign is strictly greater than the character to the left of the minus sign." Start from the opposite side, the positive situation is complicated, the letter before the number, the number before the letter, and so on.
Then write the 'ans':
for(char i=b+1;i<e;i++) { for(int j=1;j<=p2;j++) { ans+=i; } }
This loop is very clever. The first layer solves which character to fill, and the second layer solves how many characters to fill.
Then if p1=2** and it is a letter**, change the filled lowercase letters to uppercase letters. The uppercase letters don't need to be changed, and you can use the function 'toupper' here (note that it must be judged that it is a <mark class="hltr-orange">letter</mark> here, I made a mistake here and got a wrong answer).
p1=3 doesn't matter.
Finally, reverse it directly using the function 'reverse'.
The above code is wrong, 1. The '-' at the beginning and end, such as "-a-d-"
```c++
if(a[i]=='-'&&i-1>=0&&i+1<a.length())
{
ans+=solve(a[i-1],a[i+1],p1,p2,p3);
}
Consideration of special cases
- The '-' at the beginning and end, such as "-a-d-"
- Continuous '-', such as "---" This line of code solves this problem
(isalpha(b)&&isalpha(e)&&b<e||isdigit(b)&&isdigit(e)&&b<e))
- The '-' between numbers and letters, such as "1-a"
- The character on the left side of '-' is greater than the right side, such as "d-a"