UVA 11732("strcmp()" Anyone?-Trie与左兄弟右儿子)

“strcmp()” Anyone?

strcmp() is a library function in C/C++ which compares two strings. It takes two strings as input parameter and decides which one is lexicographically larger or smaller: If the first string is greater then it returns a positive value, if the second string is greater it returns a negative value and if two strings are equal it returns a zero. The code that is used to compare two strings in C/C++ library is shown below:

int strcmp(char *s, char *t)
{
    int i;
    for (i=0; s[i]==t[i]; i++)
        if (s[i]=='\0')
            return 0;
    return s[i] - t[i];
}

Figure: The standard strcmp() code provided for this problem.

 

The number of comparisons required to compare two strings in strcmp() function is never returned by the function. But for this problem you will have to do just that at a larger scale. strcmp() function continues to compare characters in the same position of the two strings until two different characters are found or both strings come to an end. Of course it assumes that last character of a string is a null (‘\0’) character. For example the table below shows what happens when “than” and “that”; “therE” and “the” are compared using strcmp() function. To understand how 7 comparisons are needed in both cases please consult the code block given above.

 

t

h

a

N

\0

 

t

h

e

r

E

\0

 

=

=

=

 

=

=

=

 

 

t

h

a

T

\0

t

h

e

\0

 

 

Returns negative value

7 Comparisons

Returns positive value

7 Comparisons

 

Input

The input file contains maximum 10 sets of inputs. The description of each set is given below:

Each set starts with an integer N (0 

Input is terminated by a line containing a single zero. Input file size is around 23 MB.

 

Output

For each set of input produce one line of output. This line contains the serial of output followed by an integer T. This T denotes the total number of comparisons that are required in the strcmp() function if all the strings are compared with one another exactly once. So for N strings the function strcmp() will be called exactly N(N-1)/2 times. You have to calculate total number of comparisons inside the strcmp() function in those N(N-1)/2 calls. You can assume that the value of T will fit safely in a 64-bit signed integer. Please note that the most straightforward solution (Worst Case Complexity O(N2 *1000)) will time out for this problem.


Sample Input

2
a
b
4
cat
hat
mat
sir
0

Output for Sample Input

Case 1: 1
Case 2: 6



Trie dp显然,问题是4000000个点62个Sigma会MLE

于是我们用边表存边,类似【左兄弟右儿子】的做法存边



#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (20071027)
#define MAXT (10+10)
#define MAXN (4001+100)
#define Sigma_size (26*2+10)
#define MAXLen (1000+10)
#define MINLen (1)
#define MAXNode (4000000+10)
#define MAXM (MAXNode)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int tt=0;
class Trie
{
public:
//	int ch[MAXNode][Sigma_size];
	int pre[MAXNode],edge[MAXM],next[MAXM],pathc[MAXM],Size;
	int v[MAXNode],f[MAXNode],siz;
	Trie(int _siz=0):siz(_siz),Size(0){MEM(pre) MEM(edge) MEM(next) MEM(v) MEM(f)}
	void mem(int _siz=0){Size=0;siz=_siz; MEM(pre) MEM(edge) MEM(next) MEM(v) MEM(f)	}
	int idx(char c)
	{
		if ('0'<=c&&c<='9') return c-'0';
		if ('a'<=c&&c<='z') return c-'a'+10;
		if ('A'<=c&&c<='Z') return c-'A'+36;
	}
	void insert(char *s)
	{
		int u=0,n=strlen(s);
		Rep(i,n)
		{
			int c=idx(s[i]);
			bool flag=0;
			Forp(u)
			{
				if (pathc[p]==c)
				{
					u=edge[p];
					flag=1; break;
				}
			}
			if (!flag)
			{
				edge[++Size]=++siz;
				pathc[Size]=c;
				next[Size]=pre[u];
				pre[u]=Size;
				u=siz;
			}
		}
		v[u]++;
	}
	void make_f(int u)
	{
		f[u]=v[u];
		Forp(u)
		{
			make_f(edge[p]);
			f[u]+=f[edge[p]];
		}
//		cout<<u<<' '<<f[u]<<' '<<v[u]<<endl;
	}
	ll dfs(int u)
	{
		ll ans=0,node_sum=v[u];
		if (v[u]) ans+=v[u]*(v[u]-1)/2*2;
		Forp(u)
		{
			int V=edge[p];
			if (V) 
			{
				ans+=f[V]*(f[V]-1)/2*2;
				ans+=node_sum*f[V];
				ans+=dfs(V);
				node_sum+=f[V]; 
			}
		}
		return ans;
	}
}T;
int n;
char s[MAXLen];
int main()
{
//	freopen("uva11732.in","r",stdin);
//	freopen(".out","w",stdout);
	while(scanf("%d",&n)==1&&n)
	{
		T.mem();
		For(i,n)
		{
			scanf("%s",s);
			T.insert(s);
		}
		T.make_f(0);
		ll ans=T.dfs(0);
		printf("Case %d: %lld\n",++tt,ans);
	}	
	return 0;
}




你可能感兴趣的:(UVA 11732("strcmp()" Anyone?-Trie与左兄弟右儿子))