/****************************************************************************** * Sample solution for the Necklace Decomposition problem in NWERC'04 * Author: Andreas Björklund * Solution: Dynamic programming. Let dyn[i] contain the index such that * S[dyn[i],i-1] is the rightmost necklace in the necklace * decomposition for S[1,i-1]. * ******************************************************************************/ #include #include #define MAXLEN (200) int n; char buf[MAXLEN+1]; int compare(char *A,int alen,char *B, int blen) { int j=0; while (jstreng[b]) who=i; } return who==0; } void recp(char* streng,int* who,int where) { int j; if (who[where]==-1) return; recp(streng,who,who[where]); printf("("); for (j=who[where];j=0;j--) { if (isNecklace(&streng[j],i-j+1)) { if (dyn[j]==-1 || compare(&streng[dyn[j]],j-dyn[j]+1,&streng[j],i-j+1)==1) { dyn[i+1]=j; } } } } recp(streng,dyn,len); } int main(void) { scanf("%d",&n); while(n--) { scanf("%s",buf); factor(buf,strlen(buf)); printf("\n"); } return 0; }