コード例 #1
0
ファイル: Q110.cpp プロジェクト: Wulonghua/leetcode
	int calHeight(TreeNode* root) {
		if (root == NULL) return 0;
		//if (root->left == NULL && root->right == NULL)
			//return 1;
		//else
			return (1 + max(calHeight(root->left), calHeight(root->right)));
	}
コード例 #2
0
ファイル: Q110.cpp プロジェクト: Wulonghua/leetcode
	bool isBalanced(TreeNode* root) {
		if (root == NULL) return true;
		if (abs(calHeight(root->left) - calHeight(root->right)) <= 1){
			return (isBalanced(root->left) && isBalanced(root->right));
		}
		else
			return false;
	}
コード例 #3
0
ファイル: tmap.c プロジェクト: wfei/hw
int calHeight(NODE *r) {
    if (r == NULL) {
	return 0;
    }
    int height;
    height = max(calHeight(r->left), calHeight(r->right)) + 1;
    return height;
}
コード例 #4
0
void SrTerrain::buildMesh(IDirect3DDevice9* pd3dDevice)  
{
	int count = mParam.mNumCols * mParam.mNumRows;
	float *h			= new float[count];
	D3DXVECTOR3* n		= new D3DXVECTOR3[count];
	D3DXVECTOR3* m		= new D3DXVECTOR3[count];
	D3DXVECTOR2* coords = new D3DXVECTOR2[count];
	memset(h, 0, sizeof(float) * count);

	calHeight(h);

	calHeightMap(h, m);

	calNormal(m, n);

	calTextureCoord(coords);

	TERRAINVERTEX* v;
	int numVerts = (mParam.mNumRows - 1) * (mParam.mNumCols - 1);
	pd3dDevice->CreateVertexBuffer(6 * numVerts * sizeof(TERRAINVERTEX), 0, D3DFVF_TERRAIN_VERTEX, D3DPOOL_MANAGED, &mVB, NULL );
	mVB->Lock( 0, 6 * numVerts * sizeof(TERRAINVERTEX), (void**)&v, 0 );
	calVertes(v, m, n, coords);
	mVB->Unlock();

	delete []h;
	delete []n;
	delete []m;
	delete []coords;
}
コード例 #5
0
int main(){
    char str[maxn];
    int i, m = 30, ans, len;
    while(scanf("%s",str)!=EOF){
        len = strlen(str);
        for(i=0;i<=len;i++) num[i]=str[i]-'a'+1;
        num[len]=0;
        da(num, len + 1, m);
        calHeight(num, len);
    }
    return 0;
}
コード例 #6
0
ファイル: poj3415.cpp プロジェクト: toposort/Exercise
int main(){
    int k;
    char str1[maxn], str2[maxn];
    while(~scanf("%d", &k)){
        if(k==0) break;
        scanf("%s%s",str1, str2);
        len1 = strlen(str1);
        len = strlen(str2);
        for(int i = 0; i < len1; i++){
            r[i] = str1[i];
        }
        r[len1] = '$';
        for(int i = 0; i < len; i++){
            r[i+len1+1] = str2[i];
        }
        len += len1+1;
        r[len] = 0;
        da(r, len+1, 150);
        calHeight(r, len);
        long long res = 0, sum;
        int head, tail;
        for(int i = 1; i <= len; i++){
            if(height[i] < k){
                sum = 0;
                head = tail = maxn;
            }
            else{
                for(int j = head; j < tail; j++){
                    if(lcp[j] > height[i]){
                        sum -= lcp[j]-height[i];
                        lcp[j] = height[i];
                    }
                    else break;
                }
                if(sa[i-1] > len1){
                    lcp[--head] = height[i];
                    sum += lcp[head]-k+1;
                }
                if(sa[i] < len1) res += sum;
            }
        }
        for(int i = 1; i <= len; i++){
            if(height[i] < k){
                sum = 0;
                head = tail = maxn;
            }
            else{
                for(int j = head; j < tail; j++){
                    if(lcp[j] > height[i]){
                        sum -= lcp[j]-height[i];
                        lcp[j] = height[i];
                    }
                    else break;
                }
                if(sa[i-1] < len1){
                    lcp[--head] = height[i];
                    sum += lcp[head]-k+1;
                }
                if(sa[i] > len1) res += sum;
            }
        }
        printf("%I64d\n", res);
    }
}
コード例 #7
0
ファイル: tmap.c プロジェクト: wfei/hw
NODE * tmap_insertR(TMAP *t, NODE *r, NODE *parent, char * name, double val, char side, char parentBalance, int depth){
    //-------------    
    //need to check whether is size-balanced each time insert
    char balanced;
    balanced = Balance(r, name, val);
    //------------

    NODE *leaf;
    depth++;
    if(r == NULL){
	leaf = (NODE *)malloc(sizeof(NODE));
	leaf->left = NULL;
	leaf->right = NULL;
	leaf->nameVal.name = name;
	leaf->nameVal.value = val;
	(*t)->size++;
	if (depth > (*t)->height) {
	    (*t)->height = depth;
	}
	((*t)->numOfInsertion)++; 
	tableInsert((*t)->hashTable, name, leaf, h_One);
	return leaf;
    }

    // the tie is broken by the name
    // (by standard alphabetical order)
    if(r->nameVal.value == val){
	if(strcmp(r->nameVal.name, name) > 0){
	    r->left = tmap_insertR(t, r->left, r, name, val, 0, balanced, depth);
	}else{
	    r->right = tmap_insertR(t, r->right, r, name, val, 1, balanced, depth);
	}
    } else if(val < r->nameVal.value){
	r->left = tmap_insertR(t, r->left, r, name, val, 0, balanced, depth);
    } else {
	r->right = tmap_insertR(t, r->right, r, name, val, 1, balanced, depth);
    }
    
    // if the nearest node is not found and the sub-tree is not balanced
    if (parentBalance == BALANCED && balanced == INBALANCED) {
	((*t)->numOfRebalance)++;
	if (parent != NULL) { // if non-root subtree is balanced
	    if (side == 0) {
		parent->left = reBalance(r);
		(*t)->height = calHeight((*t)->root);
		return parent->left;
	    } else if (side == 1) {
		parent->right = reBalance(r);
		(*t)->height = calHeight((*t)->root);
		return parent->right;
	    }
	} else {
	    (*t)->root = reBalance(r);
	    (*t)->height = calHeight((*t)->root);
	    return (*t)->root;
	}
	
    } else {
	return r;
    }
}// end of tmap_insertR