Solution to the problem of expanding the character string of Luogu P1098

Title Description In the question of "reading program writing results" in the popularity group of the prelimin...

Title Description
In the question of "reading program writing results" in the popularity group of the preliminary contest, we have given an example of string expansion: if the input string contains a string similar to "d-h" or "4-8", we will treat it as a kind of shorthand. When outputting, we will replace the minus sign with a string of continuously increasing letters or numbers, that is, outputting the above two substrings as "d" Efgh "and" 45678 ". In this problem, we add some parameter settings to make the expansion of string more flexible. The specific agreement is as follows:
(1) In the following cases, string expansion is required: in the input string, there is a minus sign "-". Both sides of the minus sign are lowercase letters or numbers, and according to the order of ASCII code, the characters on the right side of the minus sign are strictly larger than those on the left
(2) Parameter p1: expansion method. When p1=1, for letter substring, fill in lowercase letters;
When p1=2, for letter substring, fill in capital letters. In both cases, the number substring is filled in the same way. When p1=3, both alphabetic substrings and numeric strings are filled with the same asterisk "*" as the number of letters to be filled.
(3) Parameter p2: the number of repetition of padding characters. p2=k indicates that the same character needs to be filled with k consecutive characters. For example, when p2=3, the substring "d-h" should be expanded to "deeefffggggh". The characters on both sides of the minus sign do not change.
(4) Parameter p3: change to reverse order: p3=1 means to maintain the original order, p3=2 means to output in reverse order. Note that the characters at both ends of the minus sign are not included at this time. For example, when p1=1, p2=2, p3=2, the substring "d-h" should be extended to "dgffeeh".
(5) If the character to the right of the minus sign exactly follows the character to the left, delete only the middle minus sign, for example: "d-e" should be output as "de", "3-4" should be output as "34". If the characters on the right side of the minus sign are less than or equal to the characters on the left side according to the order of ASCII code, the middle minus sign shall be kept during output, for example, "d-d" shall be output as "d-d", and "3-1" shall be output as "3-1".

Input format
A total of two lines.
The first line is three positive integers separated by spaces, which represent the parameters P1, P2 and P3 in turn.
The second line is a string, consisting only of numbers, lowercase letters, and a minus sign "–". There are no spaces at the beginning and end of the line.
Output format
A total of one line, which is the expanded string.
Example of input and output
Input #1
1 2 1
abcs-w1234-9s-4zz
Output #1
abcsttuuvvw1234556677889s-4zz
Input #2
2 3 2
a-d-d
Output #2
aCCCBBBd-d

Question solution (novice code of pure c language):
The special situations in the questions include '- 67', '67', '67', etc. at the same time, it is found that if you save the answers in an array first, it is not enough to open to 5000 (the last group of data), and the penultimate group 2000 is not enough. It is simply open to 10000 (or you can record the characters every time without saving them).
Specific ideas: first, judge whether it is' - 'or a number or a character, and then directly store the latter in a new array, and then judge the former; second, judge whether it is the beginning or the end, and then judge whether it is a character or a number of the same type; finally, judge whether and how to fill it. When judging and processing characters, ascii code value is used for substitution. When filling is needed, omit '-', and then make variables k1 and k2 equal to small number + 1 and large number-1 respectively. When filling, if adjacent, fill 0
The code is as follows:

#include<stdio.h> #include<math.h> #include<string.h> char list1[110];//Receive array char list2[10000];//Save answers int p1,p2,p3;//Three indicators int iszimu(char ch)//Determine whether it is a letter { int flag; if(ch>=97&&ch<=122) flag=1; else flag=0; return flag; } int isshuzi(char ch)//Judge whether it is a number{ int flag; if(ch>=48&&ch<=57) flag=1; else flag=0; return flag; } void tianchong(char list2[],int k1,int k2,int *top2)//* top2 uses address passing to make the actual parameter change with the parameter change { int i,j; if(p1==3)//Need to print '*' without considering the positive and negative directions { for(i=k1;i<=k2;i++) { j=p2; while(j) { list2[(*top2)++]='*'; j--; } } } else if(p3==1)//Forward printing, note p3 { for(i=k1;i<=k2;i++) { j=p2; while(j) { list2[(*top2)++]=(char)i; j--; } } } else if(p3==2)//Reverse printing { for(i=k2;i>=k1;i--) { j=p2; while(j) { list2[(*top2)++]=(char)i; j--; } } } } int main() { int k1,k2,top1,top2,len1; scanf("%d%d%d",&p1,&p2,&p3); scanf("%s",list1); len1=strlen(list1); top1=0; top2=0; while(list1[top1]!='\0') { if(isshuzi(list1[top1])||iszimu(list1[top1]))//It's numbers or letters that go straight in { list2[top2++]=list1[top1]; top1++; } else//It is' - '. { if((top1-1<0)||(top1+1>len1-1))//Two ends, directly put into the answer array { list2[top2++]=list1[top1]; top1++; continue; } else if(isshuzi(list1[top1-1])&&isshuzi(list1[top1+1]))//'-' with numbers on both sides { if(list1[top1-1]<list1[top1+1])//The little one on the left needs filling { k1=((int)list1[top1-1])+1; k2=((int)list1[top1+1])-1; tianchong(list2,k1,k2,&top2); top1++; } else//Otherwise, direct deposit { list2[top2++]=list1[top1]; top1++; } } else if(iszimu(list1[top1-1])&&iszimu(list1[top1+1]))//Ditto, '-' with letters at both ends { if(list1[top1-1]<list1[top1+1])//Left side small { if(p1==1||p1==3)//Lowercase or asterisk { k1=((int)list1[top1-1])+1; k2=((int)list1[top1+1])-1; } else if(p1==2)//Capitalization { k1=((int)list1[top1-1])+1-32;//For example, 'A'+32='a ' k2=((int)list1[top1+1])-1-32; } tianchong(list2,k1,k2,&top2); top1++; } else//Ditto { list2[top2++]=list1[top1]; top1++; } } else//Both sides are not symbols of the same kind, and the answer is stored directly { list2[top2++]=list1[top1]; top1++; } } } printf("%s",list2);//Output answer return 0; }
weixin_44532612 Published 3 original articles, won praise 0, visited 42 Private letter follow

8 February 2020, 03:35 | Views: 3552

Add new comment

For adding a comment, please log in
or create account

0 comments