void createPre(TNODE *node){ if(node != NULL){ printf(" %d", node->num); createPre(node->esq); createPre(node->dir); } }
string shortestPalindrome(string s) { if (s.size() < 2) return s; int n = s.size(); //create prefix array for the first half of s vector<int> t(n / 2); string subStr = s.substr(0, n / 2); createPre(subStr, t); int cur = n - 1, ref = 0, prev = - 1; while (cur >= 0) { if (s[cur] == s[ref]) { if (ref == n / 2 - 1) break; ++ref; --cur; } else { prev = cur + t[ref - 1]; if (ref == 0) --cur; else { //cur is pointing to the last letter of palindrome, rather than the first letter after the palindrome ++prev; ref = t[ref - 1]; } } } if (prev == -1) return s; string result = s.substr(prev); reverse(result.begin(), result.end()); result += s; return result; }
int main(){ TTREE *toku = (TTREE*)malloc(sizeof(TTREE)); int casos,a,nnode,num,b; toku->root = NULL; scanf("%d", &casos); for(a=1;a<=casos;a++){ scanf("%d", &nnode); for(b=0;b<nnode;b++){ scanf("%d", &num); addnode(toku,&num); } printf("Case %d:\n", a); printf("Pre.:"); createPre(toku->root); printf("\n"); printf("In..:"); CreateIn(toku->root); printf("\n"); printf("Post:"); CreatePost(toku->root); printf("\n\n"); toku->root = NULL; } }