2014华为校招 重邮机试 2013.9.14 第二场

来源:互联网 发布:淘宝客采集工具 编辑:程序博客网 时间:2024/06/04 19:27

题目来源:http://download.csdn.net/detail/zhou20071801/6276543

 

第一题:

 

 

第二题:

 

 

第三题:

 

 

这里讨论下第三题,第三题可能比较好(时间复杂度低)的方法是采用网上所说的 后缀树、后缀数组、后缀自动状态机 (能找到大神 陈立杰 的一篇相关文章)。这里贴出个人的一种常规暴力解法,时间效率比较低。

ps:发现这个链接上博主的方法跟我的方法算法差不多: http://blog.csdn.net/ACdreamers/article/details/8534209

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<vector>#include<list>#include<stack>#include<queue>#include<string>#include<algorithm>using namespace std;int main(){#ifndef ONLINE_JUDGE    freopen("testCase-3.txt","r",stdin);#endif    char buffer[1000+1];    char inputs[20][256];    int strNum=0;    gets(buffer);    int len=strlen(buffer);    int length[20];    int minLengthIndex = INT_MAX,minLength = INT_MAX;    for(int i=0,curlen=0;i<=len;i++){        if(buffer[i]==',' || buffer[i]=='\0'){             strncpy(inputs[strNum],&buffer[i-curlen],curlen);               inputs[strNum][curlen]= '\0';             if(curlen<minLength){                 minLength=curlen;                 minLengthIndex = strNum;                              }             strNum++;             curlen = 0;        }        else{            curlen++;        }    }    for(int curLen= minLength;curLen>=1;curLen--){        for(int ptr = 0;ptr<=minLength-curLen;ptr++){            char tmp[256];            int i=0;            strncpy(tmp,&inputs[minLengthIndex][ptr],curLen);            tmp[curLen] = '\0';            for(i=0;i<strNum;i++){                if(i ==minLengthIndex)                    continue;                else{                    if(strstr(inputs[i],tmp)!=NULL)                        continue;                    else                         break;                }            }            if(i==strNum){                printf("%s\n",tmp);                return 0;            }        }    }        fclose(stdin);        return 0;}

 

0 0