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的值,也可以直接检验当前数组是否合法. # ...
随机推荐
- MOOTOOLS和JQUERY如何同时存在,解决冲突
mootools-jquery 今天在做EcStore前台的做效果时,由于Jquery的插件比较多,于是就使用了Jquery的插件,但是发现会引起Mootools的冲突. 于是猛找资料,终于找到了,现 ...
- 转:ASCII码表_全_完整版
ASCII码表 ASCII值 控制字符 ASCII值 控制字符 ASCII值 控制字符 ASCII值 控制字符 0 NUL 32 (space) 64 @ 96 . 1 SOH 33 ! 65 A 9 ...
- Android 把从网络获取的图片缓存到内存中
1:activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/androi ...
- static class
http://msdn.microsoft.com/zh-cn/library/79b3xss3(VS.80).aspx 不可被继承
- GitHub的使用详解!Windows GitHub ,Sublime Git GitGutter的使用!
Github是什么? 它是代码管理工具. 在公司写的代码,不需要U盘拷贝回家,不需要放到网盘中.只需要上传到git上,就可以回家继续拷贝下来了. 比起svn要好, svn只适合局域网工作,离开局域网, ...
- RenderBody, RenderPage and RenderSection methods in MVC 3
原文地址:http://www.codeproject.com/Articles/383145/RenderBody-RenderPage-and-RenderSection-methods-in R ...
- HDU_2047——EOF字符串排序排列问题,递推
Problem Description 今年的ACM暑期集训队一共有18人,分为6支队伍.其中有一个叫做EOF的队伍,由04级的阿牛.XC以及05级的COY组成.在共同的集训生活中,大家建立了深厚的友 ...
- 【转】C++动态创建二维数组,二维数组指针
原作者博客:蒋国宝的IT技术博客 今天完成一道题目需要自己用指针创建一个二维的数组,不得不承认指针的确是恶心. int **result; ; ; result = new int*[row]; ; ...
- Uber明年在中国将继续补贴,并大举进军100个城市!
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- Failed to start SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse
在centos7上安装nginx时,有时会遇见如题所示的nginx启动报错,一般会出现在编写完nginx启动脚本以后试着启动nginx时出现 这里其实没什么什么问题,只是很多时候我们都先用/usr/l ...