题目1:A. Chat Server'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 |
思路:
这道题需要理解其中意思,然后纯模拟,题目意思是,有三个命令 +,-,喊话
然后我们聊天喊话都要将消息发给这个房间的所有人,所以我们需要统计我们添加了多少人,这里有多组测试样例,刚开始我是因为
while(cin>> s)
{
......
}
这样去读取字符串,会遇到空格就停止读入的,然后新发现
while(getline(cin,s))
{
......
}
这样照样可以应付多组测试样例
所以根据题意,我们模拟一下就可以通过了。
代码详解如下:
#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 |
样例2:
welcome to the and |
思路:
这道题就是单纯的纯模拟,只是也是用了
while(getline(cin,s))
{
......
}
这里还要注意一下题目向上取整和交替变化左右对齐的关系
(PS:因为懒得再模拟写一遍,这个自己去理解我之前写的意思吧)
代码详解如下:
#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:::*/