百度面试题(原创)

国庆在家没事,忽然翻到以前在学校给王兄做的百度面试题,看看代码,感觉对不住王兄,没事又做了一遍~~~

1.给当前目录下的所有.cpp文件增加一个.bak后缀的备份文件
#!/bin/sh
for file in `ls`
do
        len=`expr length ${file}`
        len_beg=`expr ${len} - 3`
        if [ `expr substr ${file} ${len_beg} ${len}` = ".cpp" ]
        then
                mv ${file} ${file}.bak
        fi
done

2.任选perl、shell、python或你熟悉的一种脚本语言实现,文件words存放英文单词(单词可以重复),统计这个文件中出现次数最多的前10个单词。
#!/bin/sh
 cat word | sort | uniq -c | sort -n -r | head -n 10

3.算法工程题
XML片段的良构验证
XML是一种常用的数据交换格式。本题里为了简化难度不要求处理带属性的tag和这种形式的简写标签,将XML的良构型限制为指所有标签都闭合并且不相互嵌套。
需要验证的XML通过硬盘文件保存,在命令行参数里提供文件名。
当提供的XML片段是良构的在屏幕上打印Yes,否则打印No
选择一种语言完成上面的功能。
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <stack>

using namespace std;

int main(int argc, char** argv)
{
        if(argc != 2)
        {
                cout << "Usage: " << argv[0] << "  [filename]" << endl;
                exit(1);
        }

        string          strRead;
        char           cur_char;
        stack<string>    sta;

        ifstream xmlfile(argv[1]);

        if(!xmlfile)
        {
                cout << "Open the file fail!" << endl;
                exit(1);
        }
        strRead="";
        while(xmlfile >> cur_char)
        {
                int first =0, end = 0;
                switch(cur_char)
                {
                        case '<':
                                strRead="";
                                while(1)
                                {
                                        if(!(xmlfile >> cur_char))
                                        {
                                                cout << "No" << endl;
                                               return 0;
                                        }
                                        if(first == 0 && cur_char == '/')
                                        {
                                                first = 1;
                                                end   = 1;
                                                continue;
                                        }
                                        if(cur_char == '>')
                                                break;
                                        strRead += cur_char;
                                }
                                if(end)
                                {
                                        if(strRead != sta.top())
                                        {
                                                cout << "No" << endl;
                                                return 0;
                                        }
                                        else
                                                sta.pop();
                                }
                                else
                                        sta.push(strRead);
                                strRead="";
                                break;
                        default:
                                strRead += cur_char;
                                break;

                }
        }
        if(strRead.empty())
                cout << "Yes" << endl;
        else
                cout << "No" << endl;
        return 0;
}

4.有N(N>=1)个数组,每个数组长度不等,数组中的值均为int且都在0~MAX_VAL之间,限制需要从这N个数组中取出数值最大的前MAX_CNT_NUM个数,请给出你能想到的最优算法,包括算法思路及具体程序实现,并分析其复杂度。
算法思路:
       数组:int count[MAX_VAL]
             其中:count[i]表示i在数组中出现的次数
       打描一遍数组,填充数组count;找到前MAX_CNT_NUM个大数,最要扫描一遍count就可以。
       设数组中的总元素的数目为n
       时间复杂度:O(MAX_VAL)
       空间复杂度:O(MAX_VAL)
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <string>
#include <time.h>
using namespace std;

#define MAX_VAL       1000000
#define MAX_ELE_NUM   10
#define ARRAY_NUM     10
#define MAX_CNT_NUM   20

int main(int argc, char** argv)
{
        int*    arr[ARRAY_NUM];
        int     count[MAX_VAL]={0};
        int     num = MAX_CNT_NUM;
        time_t  t;
        srand((unsigned)time(&t));

        for(int i = 0; i < ARRAY_NUM; i++)
        {
                int num = 0;
                while( (num = rand()%MAX_ELE_NUM) == 0) {};
                arr[i] = (int*)malloc((num+1) * sizeof(int));
                arr[i][0] = num;
                cout << "ARRAY" << i << ": ";
                for(int j = 1; j <= num; j++)
                {
                        arr[i][j] = rand()%MAX_VAL;
                        cout << arr[i][j] << "  ";
                }
                cout << endl;
        }

        for(int i = 0; i < ARRAY_NUM; i++)
        {
                for(int j = 1; j <= arr[i][0]; j++)
                {
                        count[arr[i][j]]++;
                }
                free(arr[i]);
                arr[i] = NULL;
        }

        for(int i = MAX_VAL - 1; i >= 0; i--)
        {
                while(count[i] > 0)
                {
                        if(num <= 0)
                        {
                                cout << endl;
                                return 0;
                        }
                        cout << i << "  ";
                        num--;
                        count[i]--;
                }
        }

        cout << endl;
        return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值