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 |
静态数组AC代码不要用指针,因为指针开辟空间(malloc,new)都会很费时,而且指针使用后还要释放。 #include<iostream> #include<cstring> #include<cstdio> using namespace std; int ch[100005][10]; int val[100005]; int sz,flag; int getid(char ch) { return ch-'0'; } void insert(char *s) { int i,pre=0,now; for(i=0;s[i]!='\0';i++) { int id=getid(s[i]); now=ch[pre][id]; if(!now) { memset(ch[sz],0,sizeof(ch[sz])); val[sz]=0; ch[pre][id]=sz; now=sz; sz++; } if(val[now]==1) //此字符串是前面某个字符串的前缀。 { flag=1; return; } pre=now; } val[pre]=1; for(i=0;i<10;i++) //前面某个字符串是此字符串的前缀。 if(ch[pre][i]) { flag=1; return; } } int main() { int T,i,n; char str[11]; scanf("%d",&T); while(T--) { flag=0; scanf("%d",&n); sz=1; memset(ch[0],0,sizeof(ch[0])); for(i=0;i<n;i++) { scanf("%s",str); if(!flag) insert(str); } if(flag) printf("NO\n"); else printf("YES\n"); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator