題意:

有個環形的LED要顯示文字,後面的文字若和前面的文字相同的話,為了省空間可以接在一起

例如

ABC

BCD

CDE

可以接成 ABCDE

問說最少字串長需要多少

 

解法:

因為只有100*100,所以每次可以暴力檢查後面要從哪裡開始接,另外這題不用處理環狀前面相同的問題,像是

abc

def

ghi

abc

def

ghi

會串成 abcdefghiabcdefghi 而不是 abcdefghi

如果需要的話,那可以加一個KMP去算最後一個字元的前綴最長字串

 

程式碼

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int n,m;
char str[123456];
char in[123];

int main(){
    int t;
    int i,j;
    int len;
    scanf("%d",&t);
    while(t--){
        scanf("%d %d",&m,&n);
        gets(str);
        gets(str);
        --n;
        while(n--){
            gets(in);
            for(len = strlen(str),i=len-m;i<len;++i){
                for(j=i;j<len && str[j]==in[j-i];++j);
                if(j>=len)break;
            }
            strcpy(&str[i],in);
        }
        printf("%d\n",strlen(str));
    }
    return 0;
}

arrow
arrow
    全站熱搜

    alan790712 發表在 痞客邦 留言(0) 人氣()