UVALive 5792 Diccionário Portuñol
字符串匹配问题
有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的更多相关文章
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- hdu 5792(树状数组,容斥) World is Exploding
hdu 5792 要找的无非就是一个上升的仅有两个的序列和一个下降的仅有两个的序列,按照容斥的思想,肯定就是所有的上升的乘以所有的下降的,然后再减去重复的情况. 先用树状数组求出lx[i](在第 i ...
- 思维 UVALive 3708 Graveyard
题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...
- UVALive 6145 Version Controlled IDE(可持久化treap、rope)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive 6508 Permutation Graphs
Permutation Graphs Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- UVALive 6500 Boxes
Boxes Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Pract ...
- UVALive 6948 Jokewithpermutation dfs
题目链接:UVALive 6948 Jokewithpermutation 题意:给一串数字序列,没有空格,拆成从1到N的连续数列. dfs. 可以计算出N的值,也可以直接检验当前数组是否合法. # ...
随机推荐
- JQUERY1.9学习笔记 之层级选择器(四)
下一个邻居选择器(“prev ~ siblings”) 描述:选择所有"prev"后的邻居选择器,他们有相同的父节点. 注意:prev + next 与 prev ~ siblin ...
- switch case加条件语句(非等值) php
<?php $s_level=rand(1,6); echo $s_level.'<br/>'; switch(true){ case $s_level<3 : echo 'l ...
- 在线编辑器kindEditor
操作文档:http://kindeditor.net/doc.php 文件下载
- dede 日期的所有格式
[field:pubdate function=strftime('%d',@me)/] 日 [field:pubdate function=strftime('%d日',@me)/] - [fiel ...
- js常用 禁止F5 和右键
document.oncontextmenu = function() {event.returnValue = false;} //右键 document.onkeydown = function( ...
- Bug :”解压压缩文件失败: cpio; 在头中不存在归档“
问题描述: 在rpm包目录下执行rpm -ivh *rpm -force时,出现标题错误 解决办法: *src.rpm包也就源码包不能被直接进行安装,需要先将src.rpm包进行编译生成二进制的rpm ...
- 大小写转换,split分割
一.大小写转换 1.定义和用法 toUpperCase() 方法用于把字符串转换为大写. toLowerCase() 方法用于把字符串转换为小写. 用法: stringObject.toUppe ...
- ModelSim仿真入门
ModelSim仿真入门之一:软件介绍 编写这个教程之前,为了让不同水平阶段的人都能阅读,我尽量做到了零基础入门这个目标,所有的操作步骤都经过缜密的思考,做到了详细再详细的程度. 如果您是FPGA开发 ...
- d3可视化实战01:理解SVG元素特性
一. SVG简介 ————————————————————————————————————————————————————————————————— SVG是一种和图像分辨率无关的矢量图形格式,它使用 ...
- 剑指offer之关于整数的处理
首先是整数次方的处理 在这处理的时候有几个细节主义处理 1.当指数是负数的时候 2.当指数式0的时候 3.当不满足条件的时候要抛出异常 再一个就是常用的将一个树化为二进制的形式,或者是求整数的幂或者矩 ...