POJ 1635 트리의 최소는 Hash를 나타냅니다.
17769 단어 hash
Source Code
Problem: 1635
User: sunyanfei
Memory: 748K
Time: 16MS
Language: G++
Result: Accepted
/*
* problem:poj 1635
* Type:
*/
#include<iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=3005;
struct node
{
int depth,son;
}tree[maxn],tree2[maxn];
int cmp(const void* a,const void* b)
{
node c=*(node*)a;
node d=*(node*)b;
if(c.depth==d.depth)return c.son-d.son;
else return c.depth-d.depth;
}
int min_pre(char str[],node result[])
{
int node_num=1,now=0;
int father[maxn];
father[0]=result[0].son=result[0].depth=0;
for(int i=0;str[i];i++)
{
if(str[i]=='0')
{
father[node_num]=now;
result[node_num].depth=result[father[node_num]].depth+1;
result[node_num].son=0;
now=node_num++;
}
else
{
result[father[now]].son+=result[now].son+1;
now=father[now];
}
}
qsort(result,node_num,sizeof(result[0]),cmp);
return node_num;
}
int main()
{
setbuf(stdout,NULL);
char str[maxn];
int T;
cin>>T;
while(T--)
{
scanf("%s",str);
int num=min_pre(str,tree);
scanf("%s",str);
int num2=min_pre(str,tree2);
if(num!=num2)
{
printf("different
");
continue;
}
int i;
for(i=0;i<num;i++)
{
if(tree[i].depth!=tree2[i].depth|| tree[i].son!=tree2[i].son)
{
printf("different
");
break;
}
}
if(i>=num)printf("same
");
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
해시란 무엇입니까?해시 함수는 단순히 임의 길이의 입력 X를 고정 길이 n의 출력 h(x)에 매핑하는 함수입니다. ” The input to a hash function is called a pre-image, the message,...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.