Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

静态数组AC代码

Posted by 452181625 at 2014-05-26 13:39:14 on Problem 3630
不要用指针,因为指针开辟空间(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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator