(笔记)小白入门CF 多组测试样例新发现,2题


(笔记)小白入门CF 多组测试样例新发现,2题

原创 已于 2023-08-11 23:20:42 修改 · 粉丝可见 · 270 阅读 · 0 · 0 · 本内容遵循CC 4.0 BY-SA版权协议 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 · 编辑
文章链接:https://blog.csdn.net/hacker_51/article/details/132093459

题目1:A.ChatServer’s Outgoing Traffic

样例1:

+Mike
Mike:hello
+Kate
+Dmitry
-Dmitry
Kate:hi
-Kate
9

样例2:

+Mike
-Mike
+Mike
Mike:Hi   I am here
-Mike
+Kate
-Kate
14

思路:

这道题需要理解其中意思,然后纯模拟,题目意思是,有三个命令 +,-,喊话

然后我们聊天喊话都要将消息发给这个房间的所有人,所以我们需要统计我们添加了多少人,这里有多组测试样例,刚开始我是因为

1
2
3
4
while(cin>> s)
{
......
}

这样去读取字符串,会遇到空格就停止读入的,然后新发现

1
2
3
4
while(getline(cin,s))
{
......
}

这样照样可以应付多组测试样例

所以根据题意,我们模拟一下就可以通过了。

代码详解如下:

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
#include <iostream>
#include <cstring>
#include <set>
#define endl '\n'
#define sget(x) getline(cin,s)
using namespace std;

int main()
{
int ans = 0;
string s; // 输入命令
set<string>v; // 存储房间人数
while (sget(s))
{
int len = s.size();
if (s[0] == '+')
{
// 添加人名命令,这里我们也要处理一下,不要把 命令符号 + 也添加进去
v.insert(s.substr(1, len - 1));
}
else if (s[0] == '-')
{
// 删除人名命令,和添加人名命令同理
// 这也是为什么我用 set 存储人名,因为 set 可以直接根据元素值来进行删除
v.erase(s.substr(1, len - 1));
}
else
{
// 最后 计算 发送消息的长度 我们先 find 找到 符号 ":" ,然后后面就是我们的消息长度作为字节
// 再累加我们消息发送给多少人 乘以 我们消息长度即可
int pos = (int)s.find(':') + 1;
ans += v.size() * s.substr(pos, len - pos).size();
}
}
// 输出答案
cout << ans << endl;
return 0;
}

最后提交:

题目2:B. Center Alignment

样例1 :

This  is

Codeforces
Beta
Round
5

样例2:

welcome to the
Codeforces
Beta
Round 5

and
good luck

思路:

这道题就是单纯的纯模拟,只是也是用了

1
2
3
4
while(getline(cin,s))
{
......
}

这里还要注意一下题目向上取整和交替变化左右对齐的关系

(PS:因为懒得再模拟写一遍,这个自己去理解我之前写的意思吧)

代码详解如下:

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <bits/stdc++.h>
#define x first
#define y second
#define sc scanf
#define endl '\n'
#define pr printf
#define pc(x) putchar(x)
#define int long long
#pragma GCC optimize(2)
#define umap unordered_map
#define uset unordered_set
#define Hn putchar('\n')
#define KG putchar(' ')
#define mk make_pair
#define gc(x) getchar()
#define sget(x) getline(cin,(x))
#define All(x) (x).begin(),(x).end()
#define isf(x) ((x) >= 'a' && (x) <= 'z')
#define isF(x) ((x) >= 'A' && (x) <= 'Z')
#define ms0(x) memset((x),0,sizeof (x))
#define msf1(x) memset((x),-1,sizeof (x));
#define YES putchar('Y'),putchar('E'),putchar('S'),putchar('\n')
#define NO putchar('N'),putchar('O'),putchar('\n')
#define Yes putchar('Y'),putchar('e'),putchar('s'),putchar('\n')
#define No putchar('N'),putchar('o'),putchar('\n')
#define yes putchar('y'),putchar('e'),putchar('s'),putchar('\n')
#define no putchar('n'),putchar('o'),putchar('\n')
#define debug(x...) cerr << (#x) << " -> "; err(x)
#define ___G std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0)
using namespace std;

const int MOD = 1000000007;

const int N = 2e6 + 10, M = 508;

typedef pair<int, int> PII;

int ___t = 1, ___tt = N;

inline int read();

inline void write(int x);

inline void err();

template<class ___T, class ...___Ts>
inline void err(___T &x, ___Ts &...y);

template<class __T>
inline void Pv(__T x);
bool st = true;
int sz = -1;
string s;
vector<string>v;

inline void Xh(int x)
{
x += 2;
while (x--)
pc('*');
}

inline void Out(int len, string s)
{
if (len == sz)
{
cout << s;
return ;
}
len = sz - len;
int a = 0, b = 0;

if (len % 2 == 0)
{
a = len / 2;
b = a;
}
else
{
a = len / 2;
b = len / 2 + 1;
if (!st)
{
swap(a, b);
}
if (st)
st = false;
else
st = true;
}
while (a--)
KG;
cout << s;
while (b--)
KG;
}

inline void solve()
{
while (sget(s))
{
// if (s == "z")
// break;
int len = s.size();
if (sz < len)
sz = len;
v.emplace_back(s);
}
Xh(sz);
Hn;
int n = v.size();
for (int i = 0; i < n; ++i)
{
string tem = v[i];
int len = tem.size();
pc('*');
Out(len, tem);
pc('*');
Hn;
}
Xh(sz);
}

signed main()
{
//___G;
//freopen("a.txt","r",stdin);
//sc("%lld",&___t);
//cin >> ___t;
//___t = read();
while (___t--)
{
solve();
}

return 0;
}

inline int read()
{
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
x = x * 10 + ch - '0', ch = getchar();
return x * f;
}

inline void write(int x)
{
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
write(x / 10);
putchar(x % 10 + '0');
return;
}

inline void err()
{
cerr << endl;
}

template<class ___T, class ...___Ts>

inline void err(___T &x, ___Ts &...y)
{
cerr << (x) << ' ';
err(y...);
}

template<class __T>

inline void Pv(__T x)
{
for (auto i : x)
{
cerr << i << ' ';
}
Hn;
}

/* :: ::
:;J7,:, ::;7:
,ivYi,, ;LLLFS:
:iv7Y i :7ri;j5PL
,:ivYLvr ,ivrrirrY2X,
:;r@Wwz.7r: :ivu@kexianli.
:iL7::,:::iiirii:ii;::::,,irvF7rvvLujL7ur
ri::,:,::i:iiiiiii:i:irrv177JX7rYXqZEkvv17
;i:, , ::::iirrririi:i:::iiir2XXvii;L8OGJr71i
:,, ,,: ,::ir@mingyi.irii:i:::j1jri7ZBOS7ivv,
,::, ::rv77iiiriii:iii:i::,rvLq@huhao.Li
,, ,, ,:ir7ir::,:::i;ir:::i:i::rSGGYri712:
::: ,v7r:: ::rrv77:, ,, ,:i7rrii:::::, ir7ri7Lri
, 2OBBOi,iiir;r:: ,irriiii::,, ,iv7Luur:
,, i78MBBi,:,:::,:, :7FSL: ,iriii:::i::,,:rLqXv::
: iuMMP: :,:::,:ii;2GY7OBB0viiii:i:iii:i:::iJqL;::
, ::::i ,,,,, ::LuBBu BBBBBErii:i:i:i:i:i:i:r77ii
, : , ,,:::rruBZ1MBBqi, :,,,:::,::::::iiriri:
, ,,,,::::i: @arqiao. ,:,, ,:::ii;i7:
:, rjujLYLi ,,:::::,:::::::::,, ,:i,:,,,,,::i:iii
:: BBBBBBBBB0, ,,::: , ,:::::: , ,,,, ,,:::::::
i, , ,8BMMBBBBBBi ,,:,, ,,, , , , , , :,::ii::i::
: iZMOMOMBBM2::::::::::,,,, ,,,,,,:,,,::::i:irr:i:::,
i ,,:;u0MBMOG1L:::i:::::: ,,,::, ,,, ::::::i:i:iirii:i:i:
: ,iuUuuXUkFu7i:iii:i:::, :,:,: ::::::::i:i:::::iirr7iiri::
: :rk@Yizero.i:::::, ,:ii:::::::i:::::i::,::::iirrriiiri::,
: 5BMBBBBBBSr:,::rv2kuii:::iii::,:i:,, , ,,:,:i@petermu.,
, :r50EZ8MBBBBGOBBBZP7::::i::,:::::,: :,:,::i;rrririiii::
:jujYY7LS0ujJL7r::,::i::,::::::::::::::iirirrrrrrr:ii:
,: :@kevensun.:,:,,,::::i:i:::::,,::::::iir;ii;7v77;ii;i,
,,, ,,:,::::::i:iiiii:i::::,, ::::iiiir@xingjief.r;7:i,
, , ,,,:,,::::::::iiiiiiiiii:,:,:::::::::iiir;ri7vL77rrirri::
:,, , ::::::::i:::i:::i:i::,,,,,:,::i:i:::iir;@Secbone.ii:::*/

觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭

微信二维码

wechat pay

支付宝二维码

ali pay

(笔记)小白入门CF 多组测试样例新发现,2题
http://blog.angindem.cn/2023/08/11/Angindem-CSDN博客/016_16/
作者
Angindem
发布于
2023年8月11日
许可协议