思路:如题
注:数组开不好很容易在爆空间和wa中徘徊
#include <iostream>
#include <cstring>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <cstdio>
#include <stdio.h>
#include<queue>
#include<math.h>
#include<stack>
#include<set>
#include<map>
#include<bitset>
using namespace std;
inline int read()
{
int k=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
k=k*10+ch-'0';
ch=getchar();
}
return k*f;
}
#define maxnode 500005
#define sigma_size 100
#define ll long long
struct Trie
{
int sz;
int ch[maxnode][sigma_size];
int val[maxnode];
}tree;
int t;
int qqq;
int inite()
{
tree.sz = 1;
memset(tree.ch[0], 0, sizeof(tree.ch[0]));
}
int idx(char c)
{
return c -'A';
}
char p[10];
void insert_(int x)
{
scanf("%s",p);
int u = 0,n = strlen(p);
for (int i = 0; i < n; i++)
{
int c = idx(p[i]);
if (!tree.ch[u][c])
{
memset(tree.ch[tree.sz], 0, sizeof(tree.ch[tree.sz]));
tree.val[tree.sz] = 0;
tree.ch[u][c] = tree.sz++;
}
u = tree.ch[u][c];
}
tree.val[u]=x;
}
char qu[10];
int query()
{
scanf("%s",qu);
int u = 0, n = strlen(qu);
for (int i = 0; i < n; i++)
{
int c = idx(qu[i]);
if (!tree.ch[u][c])
{
return 0;
}
u = tree.ch[u][c];
}
return tree.val[u];
return 0;
}
int c[100005];
int nn;
inline int lowbit(int x) { return x & (-x); }
void add(int x, int d)
{
while (x <= nn)
{
c[x] += d;
x += lowbit(x);
}
}
ll sum(int x)
{
ll s = 0;
while (x)
{
s += c[x];
x -= lowbit(x);
}
return s;
}
int main()
{
nn=read();
inite();
for(int i=1;i<=nn;i++)insert_(i);
ll ans = 0;
for (int i = 1; i <= nn; i++)
{
int tem=query();
add(tem, 1);
ans += i - sum(tem);
}
cout<<ans<<'\n';
}