P1098. 字符串的展开

考点

  • 模拟
  • 字符串

题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <bits/stdc++.h>

using namespace std;

string s;

int p1, p2, p3;

//返回-1,删除-
//返回0,不变
//返回1,换新
int judge(int a, int b)
{
if (s[b] - s[a] == 1)
return -1;
else if (s[a] >= s[b])
return 0;
else
return 1;
}

string filling(int a, int b)
{
string res;
for (int c = s[a] + 1; c < s[b]; ++c)
{
char chr;
if (p1 == 1)
chr = (char)c;
else if (p1 == 2)
chr = toupper((char)c);
else
chr = '*';
for (int cnt = 1; cnt <= p2; ++cnt)
res += chr;
}
if (p3 == 2)
reverse(res.begin(), res.end());
return res;
}

int main()
{
int idx = 0;
cin >> p1 >> p2 >> p3;
cin >> s;
string ans;
for (int idx = 0; idx < s.length(); ++idx)
{
int head = idx - 1, tail = idx + 1, j;
if (s[idx] != '-' ||
head < 0 || tail == s.length() ||
!(isdigit(s[head]) && isdigit(s[tail]) || isalpha(s[head]) && isalpha(s[tail])))
{
ans += s[idx];
continue;
}
if ((j = judge(head, tail)) == -1)
continue;
else if (j == 0)
ans += s[idx];
else
ans += filling(head, tail);
}
cout << ans;
return 0;
}

思路

字符串的题,先思考是逐个读入方便还是整串处理方便;这道题显然只能选择整串读入,因为要处理-号的前后

每次判断-号的前一位和后一位,如果同时为数字或者同时为字母就进行下一步的扩展操作即可

唯一的坑点在于-号可能出现在开头和结尾,所以要特判范围防止越界