字节跳动2019校招第三次笔试题解

1、最长不重复子串

#include
#include
using namespace std;
int solve(const string &str){
    int hash[256]={0};
    int start=0;
    int mstart=0;
    int mlen=0;
    int idx=0;
    int len=0;
    while(idx!=str.size()){
        if(hash[str[idx]]==1){
            if(len>mlen){
                mstart=start;
                mlen=len;
            }
            while(str[start]!=str[idx]){
                hash[str[start]]=0;
                start++;
                len--;
            }
            start++;
        }
        else{
            hash[str[idx]]=1;
            len++;
        }
        idx++;
    }
    if(len>mlen){
        mlen=len;
        mstart=start;
    }
    return mlen;
}

int main()
{
    string str;
    while(cin>>str){
        cout<

2、组织优化(求矩阵连通域个数)

#include
#include
using namespace std;
 
#define size 500
int I[size][size];
int n=0,mmin,mmax;
int num = 0; 
 
bool check(int x, int y){
     if (x >= 0 && x <= n && y >= 0 && y <= n && I[x][y] != 0)
     {
         return true;
     }else
     {
         return false;
     }
}
 
int main(void)
{
   while(cin>>n){
       for (int i = 0; i < n; ++i)
       {
           for (int j=0; j< n; j++)
           {
               cin>>I[i][j];
           } 
       }

       int label = 1;

       for (int i = 0; i < n; ++i)
       {
        for (int j = 0; j < n; ++j)
            {
                 if (check(i, j))
                 {
                    if (check(i, j-1)) 
                    {
                       if (check(i-1, j))
                       {
                         mmin = I[i-1][j];
                         mmax = I[i][j-1];
                         if  (mmax < mmin) { 
                             mmin = I[i][j-1]; 
                             mmax = I[i-1][j];
                             for (int t = j; t >= 0; t--)
                             {
                                for (int w = i; w >=0; w--)
                                {
                                    if (I[w][t] == mmax) { I[w][t] = mmin; }
                                }
                             }
                              label--;
                         }
                         I[i][j] = mmin;
                         } else 
                       {
                         I[i][j] = I[i][j-1];
                       }
                    } else if (check(i-1, j))
                    {
                         I[i][j] = I[i-1][j];
                    } else
                      {
                         label ++;
                         I[i][j] = label;
                      }
                 }

             }
       }
       num = label - 1;
       cout<

3、IP还原

#include
#include
#include 
using namespace std;

void msolve(string s, int n, string out, int& res) {//out即为生成的IP地址字符串
    if (n == 4) {
        if (s.empty()) {//out字符串的IP地址合法
            ++res;
            //cout< 255 || k != to_string(val).size()) continue;
            msolve(s.substr(k), n + 1, out + s.substr(0, k) + (n == 3 ? "" : "."), res);
        }
    }
}
int solve(string s) {
    int res=0;
    msolve(s, 0, "", res);
    return res;
}

int main()
{
    string str;
    while(cin>>str){
        cout<

4、UTF-8校验

#include
#include 
#include 
using namespace std;
bool isOneByte(char i)
{
    return (i & 0x80) == 0;
}

bool isDataByte(char i)
{
    return (i & 0xC0) == 0x80;
}

int getDataCount(char i)
{
    int bit = 7;
    int count = 0;
    while(bit >= 0 && ((1 << bit) & i))
    {
        ++count;
        --bit;
    }
    return count;
}

bool solve(vector& data) {
    int k = 0;
    int count = 0;
    while(k < data.size())
    {
        char real = data[k] & 0xFF;
        if(isOneByte(real))
        {
            if(count != 0)
            {
                return false;
            }
        }
        else
        {
            if(isDataByte(real))
            {
                if(count <= 0)
                {
                    return false;
                }
                else
                {
                    --count;
                }
            }
            else
            {
                if(count != 0)
                {
                    return false;
                }
                else
                {
                    count = getDataCount(real) - 1;
                }
            }
        }
        ++k;
    }
    return count == 0;
}

int main()
{
    int n;
    while(cin>>n){
        vector v(n);
        for(auto i=0;i>v[i];
        }
        cout<

5、抖音红人

感觉是有向图求从某一顶点的可达顶点数问题,没时间写了,cout<<1<

你可能感兴趣的:(C++,算法)