Chat Server's Outgoing Traffic
题意:一个聊天室,+代表有人进去,-代表有人出去,名字:话。问你有人说话的时候要发送多少字节。
直接模拟一下就好了
#include <cstdio>
#include <string>
#include <queue>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#define ll __int64
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
char s[110];
int main()
{
int num=0,sum=0,ans;
while(gets(s))
{
if(s[0]=='+')
num++;
else if(s[0]=='-')
num--;
else
{
for(int i=0; i<strlen(s); i++)
if(s[i]==':')
{
ans=i;
break;
}
sum+=(strlen(s)-1-ans)*num;
}
}
printf("%d\n",sum);
} Center Alignment
题意:给出多行字符串,让你加边框,如果空格不能平均分配,那么一个靠左,一个靠右。暴力模拟...
#include <cstdio>
#include <string>
#include <queue>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#define ll __int64
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int main()
{
string s[1005];
int cnt=0;
int ma=0;
while(getline(cin,s[cnt]))
{
int len=s[cnt].length();
ma=max(len,ma);
cnt++;
}
for(int i=0; i<ma+2; i++)
printf("*");
puts("");
int flag=0;
for(int i=0; i<cnt; i++)
{
printf("*");
int len=ma-s[i].length();
if(len==ma)
{
for(int i=0; i<ma; i++)
printf(" ");
}
else if(len&1)
{
if(flag==0)
{
flag=!flag;
for(int j=0; j<len/2; j++)
printf(" ");
cout<<s[i];
for(int j=0; j<=len/2; j++)
printf(" ");
}
else
{
flag=!flag;
for(int j=0; j<=len/2; j++)
printf(" ");
cout<<s[i];
for(int j=0; j<len/2; j++)
printf(" ");
}
}
else
{
for(int j=0; j<len/2; j++)
printf(" ");
cout<<s[i];
for(int j=0; j<len/2; j++)
printf(" ");
}
printf("*");
puts("");
}
for(int i=0; i<ma+2; i++)
printf("*");
}Longest Regular Bracket Sequence
题意:给你一个由‘(’‘)’两个符号组成的字符串,求能够配对的最长连续子串的长度和数量。
直接dp...数组开小,.RE了一发..
#include <cstdio>
#include <cstring>
#include <stack>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
char s[1000055];
int dp[1000055]={0};
int main()
{
scanf("%s",s+1);
int len=strlen(s+1);
if(s[1]=='('&&s[2]==')')
dp[2]=2;
for(int i=3;i<=len;i++)
{
if(s[i]==')')
{
if(s[i-dp[i-1]-1]=='('&&i-dp[i-1]-1>=1)
{
dp[i]=dp[i-1]+2;
if(dp[i-dp[i]]&&i-dp[i]>=1)
dp[i]+=dp[i-dp[i]];
}
}
}
int maxn=0,maxnum=1;
for(int i=1;i<=len;i++)
{
if(dp[i]>maxn)
{
maxnum=1;
maxn=dp[i];
}
else if(dp[i]==maxn&&dp[i]!=0)
maxnum++;
}
printf("%d %d\n",maxn,maxnum);
}Follow Traffic Rules
题意:长度为l的道路,再d处有一个限速标志,限速为w,车的最大速度为v,加速度为a,当车从速度为0开始启动,最少多少时间能开完这条道路。
高中物理,分类讨论...
代码就不贴了
Bindian Signalizing
题意:有n座围成一个圈的山,站在两个山头能互相看见当且仅当两座山之间没有比他们更高的山,求能互相看见山的对数。
首先要拆环成链,将山的序列改变,第一座山是最高的山。
其次是统计对于这个序列的L数组和R数组。表示:
- First hill to the left of the x, which is strictly higher than x.
- First hill to the right of the x, which is strictly higher than x.
生成C数组:
- All hills that are as high as x and are located between x and y.
(来自官方题解)
最后统计答案。
本文解析了五道经典编程题目,包括聊天服务器流量计算、字符串居中显示、括号匹配序列、交通规则模拟及山脉视线计数问题。通过具体实例介绍了如何使用C++进行模拟、动态规划等算法实现。
329

被折叠的 条评论
为什么被折叠?



