//기억하는 것도 좋지만 기억에 의존하면 생각할 여유를 잃을 수 있음.
#include <stdio.h>
#include <string.h>//only for initializing variables.
#define MAX_N 100
#define MAX_STR 10000
//2MB 미만..
char safe[MAX_N][MAX_STR+1]; // => 1 * 10000 * 100 + 1= 1,000,001 bytes
int p[MAX_STR]; //=> 4 * 10000 => 40,000 bytes
inline int mystrlen(const char* str){int n=0;for(n=0;str[n]!='\0';n++);return n;}
int N;
void makeDoubledString(const char* org,char* dst){
int n_org = mystrlen(org);
int i=0;
for(i=0;i<n_org;i++){
dst[i]=org[i];
}
for(i=0;i<=n_org;i++){
dst[i+n_org] = org[i];
}
}
void makeRevDoubledString(const char* org,char* dst){
int n_org = mystrlen(org);
int i=0;
for(i=0;i<n_org;i++){
dst[i]=org[n_org-i-1];
}
for(i=0;i<n_org;i++){
dst[i+n_org] = org[n_org-i-1];
}
dst[i+n_org] = '\0';
}
void partialSearch(const char* N, int n){
int begin = 1, match =0;
while(begin + match < n){
if(N[begin + match] == N[match]){
match ++;
p[begin + match - 1] = match;
}else if(!match){begin ++;}
else{
begin += match - p[match - 1];
match = p[match - 1];
}
}
}
int firstSearch(const char *H, const char *N){
int lh = mystrlen(H), ln = mystrlen(N), begin =0, match =0;
partialSearch(N,ln);
while(begin < lh - ln){
if(match < ln && H[begin + match] == N[match]){
match ++;
if(match == ln){ return begin;}
}else if(!match){begin++;}
else{
begin += match - p[match -1 ];
match = p[match -1 ];
}
}
printf("Opps!
");
return 0;
}
int main(){
char doubleSafe[MAX_STR*2+1]; // => 1 * 20000 + 1= 20,001 bytes
char reverseSafe[MAX_STR +1];
int sum,i,j;
N=3;
strcpy(safe[0],"abbab");
strcpy(safe[1],"babab");
strcpy(safe[2],"ababb")\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0;
strcpy(safe[3],"bbaba");
N=2;
strcpy(safe[0],"RMDCMRCD");
strcpy(safe[1],"MRCDRMDC");
strcpy(safe[2],"DCMRCDRM");
int n_org = mystrlen(safe[0]);
for(i=0,sum=0;i<N;i++){
//시계방향
if(i%2==0){
makeDoubledString(safe[i+1],doubleSafe);
sum += firstSearch(doubleSafe,safe[i]);
}else{
//책방법
//makeDoubledString(safe[i],doubleSafe);
//sum += firstSearch(doubleSafe,safe[i+1]);
//내가푼방법..
makeDoubledString(safe[i+1],doubleSafe);
sum += n_org - firstSearch(doubleSafe,safe[i]);
}
}
printf("%d
",sum);
}