字符串匹配问题

有n个a串个m个b串,讲a的前缀和b的后缀粘在一起有多少个不同的新串。

首先求不同的前缀和后缀肯定好求了,就用字典树分别存一下a个倒过来的b。

那个问题就是解决例如,abcd,和bcde为ab串,abcde为新串的去重问题,可以看出来有四 个重复,a bcde,ab cde, abc de, abcd e,我们能够很轻易的观察到如果两串的前缀后缀的重和部分(在这里即为bcd)有n个,那个会有n+1种重复,所以只要减掉那些重合的部分就好,用字典树记录有多少以a结尾的长度大于1的前缀,和以a开头的长度大于一的后缀(这里a指任意字母),相乘就是需要减掉的部分。

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
using namespace std;
const int maxa = ;
const int cha = ;
int n, m, k;
long long num[][];
struct Tire{
int next[maxa][cha], fail[maxa], end[maxa];
int root, L;
int newnode(){
for(int i = ;i < ; i++){
next[L][i] = -;
}
end[L++] = ;
//printf("%d\n", L);
return L-;
}
void init(){
L = ;
root = newnode();
}
void insert(char buf[], int node){///printf("%s\n", buf);
int len = strlen(buf);
int now = root;
for(int i = ; i < len; i++){
if(next[now][buf[i] - 'a'] == -){
next[now][buf[i]-'a'] = newnode();
num[buf[i] - 'a'][node] ++;
if(now == ) num[buf[i] - 'a'][node] --;
}
now = next[now][buf[i]-'a'];
}
}
}ac, ac1;
char str[];
int main(){
int n, m;
while(scanf("%d%d", &n, &m), n+m){
memset(num, , sizeof(num));
ac.init();ac1.init();
for(int i = ;i < n; i++){
scanf("%s", str);
ac.insert(str, );
}
for(int k = ; k < m; k++){
scanf("%s", str);
reverse(str, str+strlen(str));
ac1.insert(str, );
}
long long ans = (long long)(ac1.L-)* (ac.L-);
//cout<<ans<<endl;
for(int i = ;i < ; i++){
ans -= num[i][]*num[i][];
}
cout<<ans<<endl;
}
} /*
1 1
aaaa
aaaa
*/

UVALive 5792 Diccionário Portuñol的更多相关文章

  1. UVALive - 4108 SKYLINE[线段树]

    UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug ...

  2. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  3. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  4. hdu 5792(树状数组,容斥) World is Exploding

    hdu 5792 要找的无非就是一个上升的仅有两个的序列和一个下降的仅有两个的序列,按照容斥的思想,肯定就是所有的上升的乘以所有的下降的,然后再减去重复的情况. 先用树状数组求出lx[i](在第 i ...

  5. 思维 UVALive 3708 Graveyard

    题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...

  6. UVALive 6145 Version Controlled IDE(可持久化treap、rope)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  7. UVALive 6508 Permutation Graphs

    Permutation Graphs Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  8. UVALive 6500 Boxes

    Boxes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Pract ...

  9. UVALive 6948 Jokewithpermutation dfs

    题目链接:UVALive 6948  Jokewithpermutation 题意:给一串数字序列,没有空格,拆成从1到N的连续数列. dfs. 可以计算出N的值,也可以直接检验当前数组是否合法. # ...

随机推荐

  1. [POJ] 1606 Jugs(BFS+路径输出)

    题目地址:http://poj.org/problem?id=1606 广度优先搜索的经典问题,倒水问题.算法不需要多说,直接BFS,路径输出采用递归.最后注意是Special Judge #incl ...

  2. Sicily 1034. Forest

    题目地址:1034. Forest 思路: 网上很多说用深搜,很任性.......发现广搜也挺好用的,实验课打的(⊙o⊙)…orz........囧. 先找根结点,根据根结点广搜深度,广搜宽度,不过要 ...

  3. Qt编程之QString 处理换行

    由于之间写过的一篇文章,Unix,windows,Mac里面的换行符不一样,导致处理也不一样,我现在要用QString以行分割(读取的文本文件的里面有换行符).所以要通吃这三种换行符 http://w ...

  4. CF- Day at the Beach

    C. Day at the Beach time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. Codeforce 221 div1

    A 只要打个表就能发现,1,6,8,9的所有排列就可以产生0~6的余数了... 所以...走不下去的时候一定要打表... #define rep(i,n) for(int i=0 ; i<(n) ...

  6. MemSQL 3.1 发布,元方,你怎么看? - V2EX

    MemSQL 3.1 发布,元方,你怎么看? - V2EX MemSQL 3.1 发布,元方,你怎么看?

  7. LINQ 用法,返回结果不是在定义时取值,而是在调用时实时取值,有意思!

    var names = new List<string> { "Nino o", "Alberto", "Juan", &quo ...

  8. CentOS 6.3下Samba服务器的安装与配置(转)

    CentOS 6.3下Samba服务器的安装与配置   一.简介 Samba是一个能让Linux系统应用Microsoft网络通讯协议的软件,而SMB是Server Message Block的缩写, ...

  9. Apache HttpComponents Client 4.0快速入门/升级-2.POST方法访问网页

    Apache HttpComponents Client 4.0已经发布多时,httpclient项目从commons子项目挪到了HttpComponents子项目下,httpclient3.1和 h ...

  10. google login page

    </pre><pre name="code" class="html"><div class="container&qu ...