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

题目来源:http://download.youkuaiyun.com/detail/zhou20071801/6276543

 

第一题:

 

 

第二题:

 

 

第三题:

 

 

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

ps:发现这个链接上博主的方法跟我的方法算法差不多: http://blog.youkuaiyun.com/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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值