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) |
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<N<4001) which denotes the total number of strings. Each of the next N lines contains one string. Strings contain only alphanumerals (‘0’… ‘9’, ‘A’… ‘Z’, ‘a’… ‘z’) have a maximum length of 1000, and a minimum length of 1.
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 times. You have to calculate total number of comparisons inside the strcmp() function in those
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 Output for Sample Input
2 a b 4 cat hat mat sir 0 | Case 1: 1 Case 2: 6
|
题意:输入n个字符串,两两调用一次strcmp(),问字符比较的总次数是多少?
思路:在trie树上每插入一个字符串,就求出该字符串和之前的字符串的比较次数。每个结点用val[]记录经过次数。则经过该结点的次数-经过目前字符串的下一字母所对应节点的经过次数可得到从这一结点后就分开的字符串数。累加即可。
原先我存图用邻接矩阵,华丽丽地TLE了。。
先上一发TLE代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 4002080
char str[4080];
#define LL long long int
struct Trie
{
int ch[maxn][62];
LL val[maxn];
int cnt;
void init()
{
memset(ch[0],0,sizeof(ch[0]));
val[0] = 0;
cnt = 1;
}
int idx(char c)
{
if(c >= 'a' && c <= 'z') return c - 'a';
if(c >= 'A' && c <= 'Z') return c - 'Z' + 26;
return 52 + c - '0';
}
int insert(char * s)
{
int ans = 0;
int len = strlen(s);
int u = 0;
for(int i = 0;i < len;i++)
{
int c = idx(s[i]);
if(!ch[u][c])
{
memset(ch[cnt],0,sizeof(ch[cnt]));
val[cnt] = 0;
ch[u][c] = cnt++;
}
ans += (val[u] - val[ch[u][c]]) * (2*i+1);
val[u]++;
u = ch[u][c];
}
ans += val[u]*(len*2+1);
val[u]++;
return ans;
}
}trie;
int main()
{
//freopen("in.txt","r",stdin);
int n,cas = 0;
while(scanf("%d",&n)!=EOF && n)
{
cas++;
trie.init();
LL ans = 0;
while(n--)
{
scanf("%s",str);
ans += (LL)(trie.insert(str));
}
printf("Case %d: %lld\n",cas,ans);
}
return 0;
}
解决办法是前向星存边法、、、然后突然就觉得自己SB了、、前向星我是会的,但一直都没想到用到trie图里、、
改成前向星还是莫名其妙TLE、、、改天再来看吧。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 4002080
char str[4080];
#define LL long long int
struct Trie
{
int ch[maxn],val[maxn],first[maxn],nxt[maxn];
int cnt;
void init()
{
memset(first,-1,sizeof(first));
val[0] = 0;
cnt = 1;
}
int idx(char c)
{
if(c >= 'a' && c <= 'z') return c - 'a';
if(c >= 'A' && c <= 'Z') return c - 'Z' + 26;
return 52 + c - '0';
}
int insert(char * s)
{
int ans = 0;
int len = strlen(s);
int u = 0;
bool flag = false;
for(int i = 0;i < len;i++)
{
int c = idx(s[i]);
int j;
for(j = first[u];j != -1;j = nxt[j])
{
if(ch[j] == c)
{
flag = true;
break;
}
}
if(!flag)
{
ch[cnt] = c;
val[cnt] = 0;
nxt[cnt] = first[u];
first[u] = cnt;
cnt++;
}
ans += (val[u] - val[cnt-1]) * (2*i+1);
val[u]++;
if(flag) u = j;
else u = cnt-1;
}
ans += val[u] * (len*2+1);
val[u]++;
return ans;
}
}trie;
int main()
{
//freopen("in.txt","r",stdin);
int n,cas = 0;
while(scanf("%d",&n)!=EOF && n)
{
cas++;
trie.init();
LL ans = 0;
while(n--)
{
scanf("%s",str);
ans += (LL)(trie.insert(str));
}
printf("Case %d: %lld\n",cas,ans);
}
return 0;
}
哭了~~~还是WA得莫名其妙!!!!
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 4002080
char str[4080];
#define LL long long int
LL ans;
struct Trie
{
int ch[maxn],val[maxn],first[maxn],nxt[maxn];
int cnt;
void init()
{
first[0] = -1;
val[0] = 0;
cnt = 1;
}
void insert(char * s)
{
int len = strlen(s);
int u = 0;
for(int i = 0;i < len;i++)
{
bool flag = false;
int c = s[i];
int j;
for(j = first[u];j != -1;j = nxt[j])
{
if(ch[j] == c)
{
flag = true;
break;
}
}
if(!flag)
{
first[cnt] = -1;
ch[cnt] = c;
val[cnt] = 0;
nxt[cnt] = first[u];
first[u] = cnt;
cnt++;
}
ans += (LL)((val[u] - val[flag?j:cnt-1]) * (2*i+1));
val[u]++;
if(flag) u = j;
else u = cnt-1;
}
ans += (LL)(val[u] * (len*2+1));
val[u]++;
}
}trie;
int main()
{
//freopen("in.txt","r",stdin);
int n,cas = 0;
while(scanf("%d",&n)!=EOF && n)
{
cas++;
trie.init();
ans = 0;
while(n--)
{
scanf("%s",str);
trie.insert(str);
}
printf("Case %d: %lld\n",cas,ans);
}
return 0;
}
好吧。。终于A了!!!
有公共前缀的比较次数是2*公共前缀+1
但是相同的串的比较次数是2*(len+1)
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 4002080
char str[4080];
#define LL long long int
LL ans;
struct Trie
{
int ch[maxn],val[maxn],first[maxn],nxt[maxn];
int cnt;
void init()
{
first[0] = -1;
val[0] = 0;
cnt = 1;
}
void insert(char * s)
{
int len = strlen(s);
int u = 0;
for(int i = 0;i < len;i++)
{
bool flag = false;
int c = s[i];
int j;
for(j = first[u];j != -1;j = nxt[j])
{
if(ch[j] == c)
{
flag = true;
break;
}
}
if(!flag)
{
first[cnt] = -1;
ch[cnt] = c;
val[cnt] = 0;
nxt[cnt] = first[u];
first[u] = cnt;
cnt++;
}
ans += (LL)((val[u] - val[flag?j:cnt-1]) * (2*i+1));
val[u]++;
if(flag) u = j;
else u = cnt-1;
}
int num = 0;
for(int j = first[u];j != -1;j = nxt[j])
num += val[j];
ans += (LL)(num * (len*2+1));
ans += (LL)((val[u]-num) * (len+1) * 2);
val[u]++;
}
}trie;
int main()
{
//freopen("in.txt","r",stdin);
int n,cas = 0;
while(scanf("%d",&n)!=EOF && n)
{
cas++;
trie.init();
ans = 0;
while(n--)
{
scanf("%s",str);
trie.insert(str);
}
printf("Case %d: %lld\n",cas,ans);
}
return 0;
}