[NOIP2007] string expansion

Posted by Bauer418 on Sun, 09 Jan 2022 11:04:36 +0100

Source: Niuke network

Time limit: 1 second for C / C + + and 2 seconds for other languages
Space limitation: C/C++ 262144K,
Other languages 524288K 64bit IO Format:%lld

Title Description

In the problem of "reading program writing results" of the preliminary popularity group, we gave an example of string expansion: if the input string contains a substring similar to "d-h" or "4-8", we regard it as a abbreviation. When outputting, we replace the minus sign with a continuously increasing letter or number string, that is, Output the above two substrings as "defgh" and "45678" respectively. In this topic, we add some parameter settings to make the string expansion more flexible. The specific agreement is as follows:
(1) In the following cases, you need to expand the string: in the input string, a minus sign "-" appears, both sides of the minus sign are lowercase letters or numbers, and according to the order of ASCII code, the characters on the right of the minus sign are strictly larger than those on the left.
(2) Parameter p1: expansion method. When p1=1, fill in lowercase letters for letter substrings; When p1=2, fill in uppercase letters for letter substrings. In both cases, the number substring is filled in the same way. When p1=3, whether it is an alphabetic substring or a numeric substring, it is filled with an asterisk "*" with the same number of letters to be filled.
(3) Parameter p2: number of repetitions of filling characters. p2=k indicates that the same character should be filled with k consecutive characters. For example, when p2=3, the substring "d-h" should be extended to "deeefffgggh". The characters on both sides of the minus sign remain unchanged.
(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 still not included. For example, when p1=1, p2=2, p3=2, the substring "d-h" should be extended to "dggffeeh".
(5) If the character on the right of the minus sign is just the successor of the character on the left, only the minus sign in the middle is deleted. For example, "d-e" should be output as "de" and "3-4" should be output as "34". If the characters on the right of the minus sign are less than or equal to the characters on the left in the order of ASCII code, the minus sign in the middle should be retained when outputting. For example, "d-d" should be output as "d-d" and "3-1" should be output as "3-1".

Enter Description:

  • The first line is three positive integers separated by spaces, representing the parameters p1, p2 and p3 in turn.
  • Line 2 is a line of string, which is only composed of numbers, lowercase letters and minus sign "-". There are no spaces at the beginning and end of the line.

Output Description:

Output a line of expanded string.

  • Example 1
    input
1 2 1
abcs-w1234-9s-4zz
  • output
abcsttuuvvw1234556677889s-4zz
  • Example 2
    input
2 3 2
a-d-d
  • output
aCCCBBBd-d
  • Example 3
    input
3 4 2
di-jkstra2-6
  • output
dijkstra2************6

remarks:

40% of the data meet: the length of the string does not exceed 5
100% of the data meet: 1 ≤ p1 ≤ 3, 1 ≤ p2 ≤ 8, 1 ≤ p3 ≤ 2.
String length does not exceed 100

WA:

#include <iostream>
#include <algorithm>
#include <cctype>
#include <cstring>
#include <cstdio>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    int p1, p2, p3;
    cin >> p1 >> p2 >> p3;
    string str;
    cin >> str;
    int len = str.size();
    cin.tie(0);
    cout.tie(0);
    for (int i = 0; i < len; i++)
    {
        if (str[i] == '-')
        {
            if (str[i - 1] >= str[i + 1])
                cout << str[i];
            else if (str[i - 1] + 1 == str[i + 1])
            {
                if (str[i] != '-')
                    cout << str[i];
            }
            else
            {
                if ((isalpha(str[i - 1]) && isalpha(str[i + 1])) || (isdigit(str[i - 1]) && isdigit(str[i + 1])))
                {
                    if (p1 == 1)
                    {
                        int cnt = str[i + 1] - str[i - 1] - 1;
                        if (p3 == 1)
                        {
                            for (int j = 1; j <= cnt; j++)
                            {
                                for (int k = 0; k < p2; k++)
                                {
                                    cout << char(str[i - 1] + j);
                                }
                            }
                        }
                        else if(p3 == 2)
                        {
                            for (int j = cnt; j > 0;j--)
                            {
                                for (int k = 0; k < p2;k++)
                                {
                                    cout << char(str[i + 1] - j);
                                }
                            }
                        }
                    }
                    if (p1 == 2)
                    {
                        int cnt = str[i + 1] - str[i - 1] - 1;
                        if(p3 == 1)
                        {
                            for (int j = 1; j <= cnt;j++)
                            {
                                for (int k = 0; k < p2;k++)
                                {
                                    cout << char(str[i - 1] + j - 32);
                                }
                            }
                        }
                        if(p3 == 2)
                        {
                            for (int j = cnt; j > 0;j--)
                            {
                                for (int k = 0; k < p2;k++)
                                {
                                    cout << char(str[i + 1] - j - 32);
                                }
                            }
                        }
                    }
                    if(p1 == 3)
                    {
                        int cnt = str[i + 1] - str[i - 1] - 1;
                        for (int j = 1; j <= cnt;j++)
                        {
                            for (int k = 0; k < p2; k++)
                            {
                                cout << "*";
                            }
                        }
                    }
                }
            }
        }
        else if(str[i] != '-')
            cout << str[i];
    }
    return 0;
}

AC:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstring>
#include <cctype>
using namespace std;
int p1, p2, p3, cnt;
int main()
{
    string str;
    cin >> p1 >> p2 >> p3;
    cin >> str;
    for (int i = 0; i < str.size(); i++)
    {
        if (str[i] != '-')
        {
            cout << str[i];
        }
        else if ((isalpha(str[i - 1]) && isalpha(str[i + 1])) || (isdigit(str[i - 1]) && isdigit(str[i + 1])))
        {
            if (str[i + 1] <= str[i - 1])
            {
                cout << str[i];
            }
            else if (str[i + 1] - str[i - 1] == 1)
            {
                if (str[i] != '-')
                    cout << str[i];
            }
            else
            {
                if (p1 == 1)
                {
                    cnt = str[i + 1] - str[i - 1] - 1;
                    if (p3 == 1) //order
                    {
                        for (int j = 1; j <= cnt; j++)
                        {
                            for (int k = 0; k < p2; k++)
                            {
                                cout << char(str[i - 1] + j);
                            }
                        }
                    }
                    else if (p3 == 1) //Reverse order
                    {
                        for (int j = 1; j <= cnt; j++)
                        {
                            for (int k = 0; k < p2; k++)
                            {
                                cout << char(str[i + 1] - j);
                            }
                        }
                    }
                }
                else if (p1 == 2)
                {
                    cnt = str[i + 1] - str[i - 1] - 1;
                    if (p3 == 1) //order
                    {
                        for (int j = 1; j <= cnt; j++)
                        {
                            for (int k = 0; k < p2; k++)
                            {
                                if (isalpha(str[i - 1]))
                                {
                                    cout << char(str[i - 1] + j - 32);
                                }
                                else
                                {
                                    cout << char(str[i - 1] + j);
                                }
                            }
                        }
                    }
                    else if (p3 == 2) //Reverse order
                    {
                        for (int j = 1; j <= cnt; j++)
                        {
                            for (int k = 0; k < p2; k++)
                            {
                                if (isalpha(str[i - 1]))
                                {
                                    cout << char(str[i + 1] - j - 32);
                                }
                                else
                                {
                                    cout << char(str[i + 1] - j);
                                }
                            }
                        }
                    }
                }
                else if (p1 == 3) //Fill '*'
                {
                    cnt = str[i + 1] - str[i - 1] - 1;
                    for (int j = 1; j <= cnt; j++)
                    {
                        for (int k = 0; k < p2; k++)
                        {
                            cout << "*";
                        }
                    }
                }
            }
        }
        else
        {
            cout << str[i];
        }
    }
    return 0;
}

Topics: Algorithm