#include #include #include typedef long long int int64; #define MAX(a,b) ((a)>(b)?(a):(b)) #define MIN(a,b) ((a)<(b)?(a):(b)) #define ABS(a) ((a)>(0)?(a):-(a)) #define POS(i,j) ((i)*(n+1)+(j)) typedef struct length{ int l; int index; } len; int cmp(const void *a,const void *b){ return -(((len *)a)->l-((len *)b)->l); } void flip(int l,char *s,int n,int *t){ int a=t[0]; int b=t[1]; if(a==t[2]){ int i; for(i=0;imax){ max=c; a[0]=i; a[1]=j; a[2]=i+l-1; a[3]=j; } while(i+lmax){ max=c; a[0]=i; a[1]=j; a[2]=i+l-1; a[3]=j; } } } for(i=0;imax){ max=c; a[0]=i; a[1]=j; a[2]=i; a[3]=j+l-1; } while(j+lmax){ max=c; a[0]=i; a[1]=j; a[2]=i; a[3]=j+l-1; } } } flip(l,s,n,a); return; } int score(const char *s,const int n){ int res=0; int i,j; for(i=0;i=parameter;i++){ int l=array[i].l; int index=array[i].index; func(l,s,n,ans+4*index); } int maxScore=score(s,n); const int m=i; int iter=100; while(iter--){ for(i=0;i<4*k;i++) tmp[i]=ans[i]; for(i=0;imaxScore){ maxScore=localScore; for(i=0;i<4*k;i++) ans[i]=tmp[i]; } else { for(i=0;imax){ max=c; a[0]=i; a[1]=j; a[2]=i; a[3]=j+l1-1; b[0]=i; b[1]=j+k; b[2]=i; b[3]=j+k+l2-1; } } } } for(i=0;i+l1max){ max=c; a[0]=i; a[1]=j; a[2]=i+l1-1; a[3]=j; b[0]=i+k; b[1]=j; b[2]=i+k+l2-1; b[3]=j; } } } } flip(l1,s,n,a); flip(l2,s,n,b); return; } void algo3(char *s,int n,len *array,int k,int *ans,int *tmp){ const int parameter=8; int i; for(i=0;i=parameter;i++); const int m=i/4*4; const int diff=m/4; for(i=0;i=parameter;i++); const int m=i/4*4; for(i=0;i=7){ if(array[i].l>array[i+1].l){ func_algo3(array[i].l,array[i+1].l,s,n,tmp+4*array[i].index,tmp+4*array[i+1].index); } else { func_algo3(array[i+1].l,array[i].l,s,n,tmp+4*array[i+1].index,tmp+4*array[i].index); } } else { func(array[i].l,s,n,tmp+4*array[i].index); func(array[i+1].l,s,n,tmp+4*array[i+1].index); } } int localScore=score(s,n); if(localScore>maxScore){ maxScore=localScore; for(i=0;i<4*k;i++) ans[i]=tmp[i]; } else { for(i=0;i