字节跳动 笔试 1题代码 3题思路

第一题AC
找厕所
思路:用num数组保存每个有厕所的商铺的位置
然后对于每个商铺去num里面二分找位置,求距离
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;


int main()
{
      int f[1000100];
      int n;
      int num[1000100];
      int len;
      int a,b;
      int p;
      string s;
      cin>>n>>s;
      for (int i=0;i<n;i++) f[i]=10000100;

      len=0;num[0]=-10000100;
      for (int i=0;i<n;i++)
            if (s[i]=='O')
            {
                  len++;
                  num[len]=i;
            }
      len++;num[len]=10000100;

      for (int i=0;i<n;i++)
            if (s[i]=='X')
            {
                  p=upper_bound(num+1,num+len+1,i)-num;
                  //cout<<"p="<<p<<endl;
                  a=i-num[p-1];
                  b=num[p]-i;
                  if (a<b) cout<<a<<' ';
                  else cout<<b<<' ';
            }
            else cout<<0<<' ';
      return 0;
}

第二题:
做题
没看,盲猜DP

第三题:
找老板签字
一个有向无环无权值图的拓扑排序,输入处理可以用stringstream
但是字典序最小怎么找啊!我要傻了
#include<iostream>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<sstream>
#include<string>
#define M 100010
using namespace std;

struct node
{
      int to,bro;

}edge[M];

int head[M];
int inn[M],ott[M],pre[M];
int tail,n;
int begino,endo;
bool check[M];

void build(int a,int b)
{
      edge[tail].to=b;
      edge[tail].bro=head[a];
      head[a]=tail++;
}

void SPFA(int s)
{
      int ans[M];
      int len;

      queue<int>q;
      q.push(s);
      check[s]=true;
      len=1;
      ans[1]=s;
      int num[M];
      int x;

      while (!q.empty())
      {
            int u=q.front();
            q.pop();
            check[u]=false;
            len++;
            ans[len]=M;
            x=0;
            cout<<u<<' ';
            for (int i=head[u];i!=-1;i=edge[i].bro)
            {
                  int v=edge[i].to;
                  ott[u]--;
                  inn[v]--;
                  if (inn[v]==0 && !check[v])
                  {
                        check[v]=true;
                        x++;
                        num[x]=v;
                  }
            }
            sort(num+1,num+1+x);

            for (int i=1;i<=x;i++) q.push(num[i]);
      }
}

int main()
{
      int x;
      int people;
      string s;
      stringstream ss;

      memset(inn,0,sizeof(inn));
      memset(ott,0,sizeof(ott));

      memset(pre,-1,sizeof(pre));
      memset(head,-1,sizeof(head));

      memset(check,0,sizeof(check));

      n=0;
      tail=0;

      while (cin>>people)
      {
            n++;
            getline(cin,s);
            ss.clear();
            ss.str(s);
            while (1)
            {
                  ss>>x;
                  if (ss.fail()) break;
                  inn[people]++;
                  ott[x]++;
                  build(x,people);
            }
      }

      for (int i=1;i<=n;i++)
      {
            if (inn[i]==0) begino=i;
            if (ott[i]==0) endo=i;
      }

      SPFA(begino);

      return 0;
}


#笔试题目##字节跳动#
全部评论
第二题不用猜啊兄弟,n只有1000那么大,直接暴力排序找就行啊😂
点赞 回复 分享
发布于 2019-09-22 10:13
第二题直接一个优先队列就行了啊。😂
点赞 回复 分享
发布于 2019-09-22 10:13
大佬
点赞 回复 分享
发布于 2019-09-22 10:10

相关推荐

05-09 12:23
已编辑
华南理工大学 Java
野猪不是猪🐗:给他装的,双九+有实习的能看的上这种厂我直接吃⑨✌们拿它练练面试愣是给他整出幻觉了
点赞 评论 收藏
分享
二月份来北京实习,虽然提前做了攻略,但是是人生第一次经历租房,全程自己搞定,所以难免还是踩坑了😅奉劝大家,租房不要只看自己的房间如何,还要看别人的房门口环境如何因为我就是那个倒霉蛋,我旁边房间额门口堆了一大堆杂物,都是另一个房间的人放在外面的,而且他门口放了几十双鞋子,冬天还好,现在是夏天,可太味了,怎么有这么多鞋啊啊啊啊,请看图片O(≧口≦)O一开始这屋里是一个人住,后来变成两个人住(他说他妈妈来北京看病暂时住,ok能体谅的)但是大概一个多月以后他妈妈回家了,无缝衔接了一位女朋友接着住,而且他的女朋友巨能买东西,我真的不得不吐槽,🥲我不管别人怎么花钱的,但是你买东西你起不来,你能不能换个时间约送上门,总是在周内或者周末的某一天早上七点多,没到起来的时间,快递员框框敲门了!!!!而且经常点外卖但是我们楼下有门禁,外卖员按响铃他俩不去解锁,一直等一直等,等到我们其他人受不了去帮解锁,吵得要死,他们像聋了一样!!!服啦!!!我的房租一个月不到1600,但是是阳隔,很不隔音,隔壁的大哥有时候会半夜吃薯片或者嗑瓜子(凌晨两三点的时候)好几次我都从梦里被嗑出来了🥲还好还剩两个月就实习结束可以回家了,呜呜想家,想我自己的窝😭
码农索隆:这也是我不喜欢合租的原因
我的租房踩坑经历
点赞 评论 收藏
分享
看到这个内容真是闹麻了。。。。。。现在有了AI以后很多人面试都会作弊吗?&nbsp;那对老老实实面试的人岂不是不公平....
重生之我要干前端:放宽心,作弊很明显的,面试官也不是傻子,而且这世上更多的肯定是依靠自己的知识的人,所以放宽心提升自己最重要
点赞 评论 收藏
分享
评论
点赞
4
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务