1387 最長重複子串

來源:互聯網
上載者:User
描述

對於一個字串S1,其中S2是他的一個子串(長度嚴格小於S1長度),如果S2在S1中出現次數超過1次,那麼S2就是一個重複子串,現在的要求是給定S1,請求出他的最長重複子串;

如果有多個長度一樣的最長子串,請輸入字典序最小那個串;

比如bbbaaaccc

那麼最長子串就是aa

輸入

第一行包含一個整數T,表示有T組資料

對於每組資料包含一行,該行有一個字串,長度小於10,000

輸出

對於每組資料請輸出他的最長重複子串,保證每組資料都有;

範例輸入
2
abacabac
abacabbac
範例輸出
abac

bac

#include <stdio.h>#include <stdlib.h>#include <string.h>#define MAXCHAR 10010 char c[MAXCHAR], *a[MAXCHAR];int comlen( char *p, char *q ){int i = 0;while( *p && (*p++ == *q++) ){++i;}return i;}int pstrcmp( const void *p1, const void *p2 ){return strcmp( *(char* const *)p1, *(char* const*)p2 );}int main( ){char ch;int n=0;int i, temp;int maxlen=0, maxi=0;int number,t;scanf("%d",&number);getchar();for(t=1;t<=number;t++){n=0;maxlen=0;maxi=0;while( (ch=getchar())!='\n' ){a[n]=&c[n];c[n++]=ch;}c[n]='\0';qsort( a, n, sizeof(char*), pstrcmp );for(i=0; i<n-1; ++i ){temp=comlen( a[i], a[i+1] );if( temp>maxlen ){maxlen=temp;maxi=i;}}printf("%.*s\n",maxlen, a[maxi]);}//system("PAUSE");return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.