P1098 [NOIP 2007 提高组] 字符串的展开

题目描述

在初赛普及组的“阅读程序写结果”的问题中,我们曾给出一个字符串展开的例子:如果在输入的字符串中,含有类似于 d-h 或者 4-8 的字串,我们就把它当作一种简写,输出时,用连续递增的字母或数字串替代其中的减号,即,将上面两个子串分别输出为 defgh45678。在本题中,我们通过增加一些参数的设置,使字符串的展开更为灵活。具体约定如下:

(1) 遇到下面的情况需要做字符串的展开:在输入的字符串中,出现了减号 - ,减号两侧同为小写字母或同为数字,且按照 ASCII 码的顺序,减号右边的字符严格大于左边的字符。

(2) 参数 :展开方式。 时,对于字母子串,填充小写字母; 时,对于字母子串,填充大写字母。这两种情况下数字子串的填充方式相同。 时,不论是字母子串还是数字字串,都用与要填充的字母个数相同的星号 * 来填充。

(3) 参数 :填充字符的重复个数。 表示同一个字符要连续填充 个。例如,当 时,子串d-h 应扩展为 deeefffgggh。减号两边的字符不变。

(4) 参数 :是否改为逆序: 表示维持原来顺序, 表示采用逆序输出,注意这时候仍然不包括减号两端的字符。例如当 时,子串 d-h 应扩展为 dggffeeh

(5) 如果减号右边的字符恰好是左边字符的后继,只删除中间的减号,例如:d-e 应输出为 de3-4 应输出为 34。如果减号右边的字符按照 ASCII 码的顺序小于或等于左边字符,输出时,要保留中间的减号,例如:d-d 应输出为 d-d3-1 应输出为 3-1

输入格式

共两行。

行为用空格隔开的 个正整数,依次表示参数

行为一行字符串,仅由数字、小写字母和减号 - 组成。行首和行末均无空格。

输出格式

共一行,为展开后的字符串。

输入输出样例 #1

输入 #1

1 2 1
abcs-w1234-9s-4zz

输出 #1

abcsttuuvvw1234556677889s-4zz

输入输出样例 #2

输入 #2

2 3 2
a-d-d

输出 #2

aCCCBBBd-d

说明/提示

的数据满足:字符串长度不超过

的数据满足:。字符串长度不超过

NOIP 2007 提高第二题

解题思路

简单模拟

AC代码

#include <bits/stdc++.h>
#include <string>
using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;
using u128 = unsigned __int128;
#define lowbit(x) = ((x) & -(x))
#define rep_0(a, b, c) for (int a = b; a < c; a++)
#define rep_1(a, b, c) for (int a = b; a <= c; a++)
#define per(a, b, c) for (int a = b; a >= c; a--)
using namespace std;

void solve()
{
    int p1, p2, p3;
    cin >> p1 >> p2 >> p3;
    string s;
    cin >> s;
    for (int i = 0; s[i]; i++)
    {
        if (s[i] == '-')
        {
            if (s[i - 1] <= 'z' && s[i - 1] >= 'a' && s[i + 1] <= 'z' && s[i + 1] >= 'a')//处理字母
            {
                if (s[i - 1] < s[i + 1])
                {
                    if (s[i + 1] == s[i - 1] + 1)
                    {
                        s.erase(i, 1);
                    }
                    else
                    {
                        int temp = (s[i + 1] - s[i - 1] - 1) * p2;
                        if (p1 == 1)
                        {
                            s.erase(i, 1);
                            string temp_s;
                            char char_s;
                            int jishu = 1;
                            for (int j = 1; j <= temp; j++)
                            {
                                char_s = (s[i - 1] + jishu);
                                temp_s = temp_s + char_s;
                                if (j % p2 == 0)
                                {
                                    jishu++;
                                }
                            }
                            if (p3 == 1)
                            {
                                s.insert(i, temp_s);
                            }
                            else if (p3 == 2)
                            {
                                reverse(temp_s.begin(), temp_s.end());
                                s.insert(i, temp_s);
                            }
                        }
                        else if (p1 == 2)
                        {
                            s.erase(i, 1);
                            string temp_s;
                            char char_s;
                            int jishu = 31;
                            for (int j = 1; j <= temp; j++)
                            {
                                char_s = (s[i - 1] - jishu);
                                temp_s = temp_s + char_s;
                                if (j % p2 == 0)
                                {
                                    jishu--;
                                }
                            }
                            if (p3 == 1)
                            {
                                s.insert(i, temp_s);
                            }
                            else if (p3 == 2)
                            {
                                reverse(temp_s.begin(), temp_s.end());
                                s.insert(i, temp_s);
                            }
                        }
                        else if (p1 == 3)
                        {
                            s.erase(i, 1);
                            string temp_s;

                            for (int j = 1; j <= temp; j++)
                            {

                                temp_s = temp_s + '*';
                            }
                            s.insert(i, temp_s);
                        }
                    }
                }
                else if (s[i - 1] >= s[i + 1])
                {
                }
            }
            else if (s[i - 1] <= '9' && s[i - 1] >= '0' && s[i + 1] <= '9' && s[i + 1] >= '0')//处理数字
            {
                if (s[i - 1] < s[i + 1])
                {
                    if (s[i + 1] == s[i - 1] + 1)
                    {
                        s.erase(i, 1);
                    }
                    else
                    {
                        int temp = (s[i + 1] - s[i - 1] - 1) * p2;
                        if (p1 == 1)
                        {
                            s.erase(i, 1);
                            string temp_s;
                            char char_s;
                            int jishu = 1;
                            for (int j = 1; j <= temp; j++)
                            {
                                char_s = (s[i - 1] + jishu);
                                temp_s = temp_s + char_s;
                                if (j % p2 == 0)
                                {
                                    jishu++;
                                }
                            }
                            if (p3 == 1)
                            {
                                s.insert(i, temp_s);
                            }
                            else if (p3 == 2)
                            {
                                reverse(temp_s.begin(), temp_s.end());
                                s.insert(i, temp_s);
                            }
                        }
                        else if (p1 == 2)
                        {
                            s.erase(i, 1);
                            string temp_s;
                            char char_s;
                            int jishu = 1;
                            for (int j = 1; j <= temp; j++)
                            {
                                char_s = (s[i - 1] + jishu);
                                temp_s = temp_s + char_s;
                                if (j % p2 == 0)
                                {
                                    jishu++;
                                }
                            }
                            if (p3 == 1)
                            {
                                s.insert(i, temp_s);
                            }
                            else if (p3 == 2)
                            {
                                reverse(temp_s.begin(), temp_s.end());
                                s.insert(i, temp_s);
                            }
                        }
                        else if (p1 == 3)
                        {
                            s.erase(i, 1);
                            string temp_s;

                            for (int j = 1; j <= temp; j++)
                            {

                                temp_s = temp_s + '*';
                            }
                            s.insert(i, temp_s);
                        }
                    }
                }
                else if (s[i - 1] >= s[i + 1])
                {
                }
            }
        }
    }
    cout << s;
    return;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
    // cin >> t;

    while (t--)
    {
        solve();
    }

    return 0;
}