字符串匹配问题

有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. Swift—析构函数-备

    与构造过程相反,实例最后释放的时候,需要清除一些资源,这个过程就是析构过程.在析构过程中也会调用一种特殊的方法deinit,称为析构函数.析构函数deinit没有返回值,也没有参数,也不需要参数的小括 ...

  2. Go Cookie 练习

    package main import ( "io" "log" "net/http" ) func main() { http.Handl ...

  3. nosql newsql

    http://www.cnblogs.com/end/archive/2011/10/19/2217244.html http://www.csdn.net/article/2011-09-26/30 ...

  4. AcitonBar 自定义布局

    Android系统中ActionBar默认的布局不美观且难于控制,通过为ActionBar自定义布局的方式可以灵活控制ActionBar. 自定义Activity主题和ActionBar样式 在新建的 ...

  5. 超给力的HTML5 3D动画欣赏及源码下载

    HTML5有着非常巨大的魅力,尤其是CSS3和Cavnas,可以帮助页面渲染得非常炫酷.值得一提的是,利用HTML5的3D特性可以帮助你更加方便地在网页上实现3D动画特效.本文分享的这些HTML5 3 ...

  6. matrix矩阵求逆 与解方程模板 留做备用 (有bug,待补充)

    // // main.cpp // 矩阵求逆 // // Created by 唐 锐 on 13-6-20. // Copyright (c) 2013年 唐 锐. All rights reser ...

  7. 写两个线程,一个对n每次加一,一个对n每次减一

    public class A{     private static Integer n = 0; }     public class B extends A implements Runnable ...

  8. spring aop实现原理

    通过两种代理方式,一是JDK本身的代理方式,二是CGLIB提供的代理方式,在代理类的前面加事务begin,在后面加事务commit,需要的数据库连接从ThreadLocal中取

  9. mongodb的应用场景

    这篇文章总结的比较到位:http://www.tuicool.com/articles/YnmaAj

  10. nginx 配置自签名的ssl证书

    最近要搭一个https的测试环境,使用nginx做反向代理. 网上找过不少资料,但过程不是很完整,吃了不少亏,故把自己的操作过程总结下来.如果你刚好遇到这个问题,希望对你有帮助! ********** ...