字符串匹配问题

有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. switch case加条件语句(非等值) php

    <?php $s_level=rand(1,6); echo $s_level.'<br/>'; switch(true){ case $s_level<3 : echo 'l ...

  2. eclipse下编译openfire3.9.1源码

    [一].下载源码 打开网址:http://www.igniterealtime.org/downloads/source.jsp 选择目前最新版本 openfire_src_3_9_1.zip 下载. ...

  3. PIN的经验和技巧

    以前总是吧PIN码排序弄成:9876543210或0123456789, 有的路由PJ好久还没出密码,但还是PJ了3~4个,大多数是抓跑, 所以,还是建议大家先跑包,实在跑不出就来PIN, 一般人的设 ...

  4. Linux/Ubuntu下 静态编译Qt程序

    一般情况下,我们用Qt编译出来的程序是要依赖于系统Qt库的,也就是这个程序移到别的没有安装Qt库的系统上是不能使用的.会提示缺少……库文件之类的错误.这就是动态编译的结果. 但是如果我们想编译一个程序 ...

  5. 学习下关于ViewStub实例的用法及带Drawable的TextView的妙用

    在项目中,我们可能有多种数据来源比如: 里面有ListView也有当获得数据为空的时候显示的空信息.根据点击的项目还是差事不同,显示的空消息也不同.a.没有收藏的项目,b目前没有收藏的差事. 其实实现 ...

  6. HDU_2040——判断亲和数

    Problem Description 古希腊数学家毕达哥拉斯在自然数研究中发现,220的所有真约数(即不是自身的约数)之和为: 1+2+4+5+10+11+20+22+44+55+110=284.  ...

  7. HDU_2020——按绝对值排序

    Problem Description 输入n(n<=100)个整数,按照绝对值从大到小排序后输出.题目保证对于每一个测试实例,所有的数的绝对值都不相等.   Input 输入数据有多组,每组占 ...

  8. Linux下安装GCC5.3.0(亲测有效)

    对于linux小白来说,只需要先知道怎么安装,至于为什么等学了linux再说吧..知识不系统的坏处啊! 首先,一般ubuntu上都预装了低级版本的Gcc,完全可以应付刷OJ时的C+Class+STL的 ...

  9. vs2010 sp1 创建silverlight 时,提示我 “在创建silverlight项目之前,您需要安装最新的silverlight Developer运行时

    ---恢复内容开始--- Silverlight 5 Developer Rumtime (32bit): http://go.microsoft.com/fwlink/?LinkId=229323 ...

  10. Hadoop2.4.1 64-Bit QJM HA and YARN HA + Zookeeper-3.4.6 + Hbase-0.98.8-hadoop2-bin HA Install

    Hadoop2.4.1 64-Bit QJM HA and YARN HA Install + Zookeeper-3.4.6 + Hbase-0.98.8-hadoop2-bin HA(Hadoop ...