Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
为什么用指针就可以对,用变量模拟指针就会WA呢?诚恳地求大神帮助,感激不尽!QAQ 我是用hash实现本题的,但是一直都WA,于是我找了别人的代码来看,这样就可以AC: #include<algorithm> #include<iostream> #include<cstring> #include<climits> #include<cstdio> using namespace std; int T,t; char s[2][3100],l1,l2; char *p; int h1,h2,hx[11000]; int has(int dep){ int re=hx[dep+5000]; while(*p&&*p++=='0')//指针模拟,实现递归 re=(re+hx[dep]*has(dep+1))%19001;//计算每棵子树的hash值 return re*re%19001; } void init(){for(int i=0;i<=10000;++i)hx[i]=rand()%19901;}//随机一些hash种子 int main() { scanf("%d",&T);init(); while(T--){ scanf("%s%s",s[0],s[1]); p=s[0];h1=has(1);p=s[1];h2=has(1); if(h1==h2)puts("same"); else puts("different"); } return 0; } 我原来的WA代码是这么写的: #include<algorithm> #include<iostream> #include<cstring> #include<climits> #include<cstdio> using namespace std; int T,t; char s[2][3100],l1,l2; int h1,h2,hx[11000]; int has(int x,int dep,int l){ int re=hx[dep+5000]; while(t<l&&s[x][t++]=='0')//用全局变量t实现移动,只有这里和上一份代码不同 re=(re+hx[dep]*has(x,dep+1,l))%19001; return re*re%19001; } void init(){for(int i=0;i<=10000;++i)hx[i]=rand()%19901;} int main() { init(); scanf("%d",&T); while(T--){ scanf("%s%s",s[0],s[1]); l1=strlen(s[0]),l2=strlen(s[1]); t=0,h1=has(0,1,l1),t=0,h2=has(1,1,l2); if(h1==h2)puts("same"); else puts("different"); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator