poj 3080 字符串

题目原述

处理第一行 fi , 从len = 60 到 3 ,起点从i =0 ...m-1 枚举所有子串,跟1...n-1 行去比较;

函数 strncpy (目的存储地址,被复制的起始地址,长度) ; 最好手动加 目的数组【len】= '\0' 

strcmp(s1,s2)  : ==1 代表s1 字典序大于 s2 ;

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<list>
#include<map>
#include<set>
using namespace std;
const int m=60;
char fi[m+10];
char str[13][m+10];
int n;
char ans[m+10],tp[m+10];
int la;
//bool OK(int r,int j){
//    int st=0;
//    while(tp[st]){
//        if(tp[st]!=str[r][j])
//            return 0;
//        else
//             st++,j++;
//    }
//    return 1;
//}
//bool check(){
//
//    for(int r=1;r<n;r++){
//        bool f1=1,f2=1;
//        for(int j=0;j<m;j++){
//            if(str[r][j]==tp[0]){
//               f1=0;
//               if(OK(r,j)){
//                  f2=0;
//                  break;
//               }
//            }
//        }
//        if(f1 || f2) return 0;
//    }
//    return 1;
//}
bool check(){
    for(int r=1;r<n;r++){
        bool f=1;
        for(int j=0;j<m;j++){
          if(strstr(str[r]+j,tp)) f=0;
        }
        if(f) return 0;
    }
    return 1;
}
void deal(){
    la=0;
    ans[0]=0;

    for(int len=m;len>2;len--){
        for(int i=0;i<=m-len;i++){
            strncpy(tp,fi+i,len);
            tp[len]=0;
            if(check()){
              if(la==0) {
                    strcpy(ans,tp);
                    la=len;
              }
              else if(strcmp(ans,tp)>0) strcpy(ans,tp);

            }
        }
        if(la) return ;
    }
}
int main()
{

//     freopen("in.in","r",stdin);
     int T;
     scanf("%d",&T);
     while(T--){
        scanf("%d",&n);
        scanf("%s",fi);
        for(int i=1;i<n;i++)
            scanf("%s",str[i]);
        deal();
        if(la<3) printf("no significant commonalities\n");
        else printf("%s\n",ans);

     }
     return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值