题意:

给出n个A串和m个B串,将这A串与B串连接(B接在A后面)可以生成n*m个AB串,求不同的AB串的数量

分析:

set直接水过

#include <bits/stdc++.h>
using namespace std; char str1[2000][15],str2[2000][15]; int main()
{
// freopen("in.txt","r",stdin);
int m,n,t,kase=0;
scanf("%d",&t);
set<string>M;
while(t--)
{
scanf("%d%d",&n,&m);
getchar();
for(int i=0; i<n; i++)
gets(str1[i]);
for(int i=0; i<m; i++)
gets(str2[i]);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
char tmps[30];
strcpy(tmps,str1[i]);
strcat(tmps,str2[j]);
M.insert(tmps);
}
}
printf("Case %d: %d\n",++kase,M.size());
M.clear();
}
return 0;
}

hash可以节省时间

#include <bits/stdc++.h>
using namespace std; const int maxn=2300000;
const int mod=2299963;
int a[maxn];
char vis[maxn][30];
char str1[2000][15],str2[2000][15]; int BKDRHash(char *str)
{
int seed = 131; // 31 131 1313 13131 131313 etc..
int res = 0;
while (*str)
res = res * seed + (*str++);
return (res & 0x7FFFFFFF)%mod;
} int APHash(char *str)
{
int res = 0;
int i;
for (i=0; *str; i++)
{
if ((i & 1) == 0)
res ^= ((res << 7) ^ (*str++) ^ (res >> 3));
else
res ^= (~((res << 11) ^ (*str++) ^ (res >> 5)));
}
return (res & 0x7FFFFFFF)%mod;
} int Judge(char *str) //两次hash
{
int h1=BKDRHash(str);
int h2=APHash(str);
while(a[h1])
{
if(a[h1]==h2) return 0;
h1++;
}
a[h1]=h2;
return 1;
} int Insert(char *str)
{
int h=BKDRHash(str);
while(a[h])
{
if(strcmp(vis[h],str)==0) return 0;
h++; //如果冲突往后移一位
}
a[h]=1;
strcpy(vis[h],str);
return 1;
} int main()
{
// freopen("in.txt","r",stdin);
int m,n,t,kase=0;
scanf("%d",&t);
while(t--)
{
memset(a,0,sizeof(a));
scanf("%d%d",&n,&m);
getchar();
for(int i=0; i<n; i++)
gets(str1[i]);
for(int i=0; i<m; i++)
gets(str2[i]);
int ans=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
char tmps[30];
strcpy(tmps,str1[i]);
strcat(tmps,str2[j]);
// if(Judge(tmps)) ans++;
if(Insert(tmps)) ans++;
}
}
printf("Case %d: %d\n",++kase,ans);
}
return 0;
}

UVA 10887 set或hash的更多相关文章

  1. UVA 10887 Concatenation of Languages 字符串hash

    题目链接:传送门 题意: 给你两个集合A,B,任意组合成新的集合C(去重) 问你最后C集合大小 题解: 暴力 组成的新串hash起来 #include<bits/stdc++.h> usi ...

  2. UVA 257 - Palinwords(弦HASH)

    UVA 257 - Palinwords 题目链接 题意:输出一个文本里面的palinword,palinword的定义为.包括两个不同的回文子串,而且要求回文子串不能互相包括 思路:对于每一个单词推 ...

  3. UVa 11019 Matrix Matcher - Hash

    题目传送门 快速的vjudge传送门 快速的UVa传送门 题目大意 给定两个矩阵S和T,问T在S中出现了多少次. 不会AC自动机做法. 考虑一维的字符串Hash怎么做. 对于一个长度为$l$的字符串$ ...

  4. uva 10887

    是个 hash  用的容器类水过 #include <iostream> #include <cstdio> #include <string> #include ...

  5. UVA 257 Palinwords(hash)题解

    思路:给你字符串,如果他包含至少两个长度大于等于3的回文,并且这些回文不能嵌套(例如aaa嵌套在aaaa,waw嵌套在awawa),如果这个字符串这么牛逼的话,就输出他. 思路:拿到字符串先正序has ...

  6. UVA - 11019 Matrix Matcher hash+KMP

    题目链接:传送门 题解: 枚举每一行,每一行当中连续的y个我们hash 出来 那么一行就是 m - y + 1个hash值,形成的一个新 矩阵 大小是 n*(m - y + 1), 我们要找到x*y这 ...

  7. 几道hash题

    1: UVa 10887 - Concatenation of Languages map 可以做 ,但是输入实在恶心,有空串之类的HASH模板: int Hash(char *s){   int s ...

  8. UVa 10029 hash + dp

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. UVA 10798 - Be wary of Roses (bfs+hash)

    10798 - Be wary of Roses You've always been proud of your prize rose garden. However, some jealous f ...

随机推荐

  1. 马哥Linux--elasticsearch

    ELK stack: Lucene: 文档:document 包含了一个或多个域的容器 field:value 域: 有很多选项 索引选项,存储选项,域向量使用选项 索引选项用于通过倒排索引来控制文本 ...

  2. CentOS 下解决ssh登录 locale 警告

    最近登录一台CentOS 6机器,发现每次登录都提示如下警告: -bash: warning: setlocale: LC_CTYPE: cannot change locale (en_US.UTF ...

  3. 链路追踪_SkyWalking的部署及使用

    关于链路追踪,目前比较主流是Cat,Zipkin,SkyWalking等这些工具.这篇文章主要介绍关于SkyWalking工具的. 为什么用SkyWalking,因为它基本没有代码侵入,只这一点就足够 ...

  4. KVO后[obj class]与object_getClass(id obj)的结果竟会不一致?

    说说背景,研究下面的代码时,KVO后[obj class]与object_getClass(id obj)的结果竟会不一致? PersonModel *aPersonModel = [[PersonM ...

  5. 能够划分局域网的VLAN

    VLAN与三层交换机 1. VLAN的概述与优势 1.1 分割广播域 1.2 VLAN的优势 2. VLAN的种类 2.1 静态VLAN 2.2 动态VLAN 3. VLAN的ID 4.三层交换机转发 ...

  6. python实现发送微信消息

    import json from threading import Timer from wxpy import * import requests import urllib.parse def g ...

  7. Go语言中的单例模式

    Go语言中的单例模式 在过去的几年中,Go语言的发展是惊人的,并且吸引了很多由其他语言(Python.PHP.Ruby)转向Go语言的跨语言学习者. Go语言太容易实现并发了,以至于它在很多地方被不正 ...

  8. Action4D:人群和杂物中的在线动作识别:CVPR209论文阅读

    Action4D:人群和杂物中的在线动作识别:CVPR209论文阅读 Action4D: Online Action Recognition in the Crowd and Clutter 论文链接 ...

  9. NVIDIA GPUs上深度学习推荐模型的优化

    NVIDIA GPUs上深度学习推荐模型的优化 Optimizing the Deep Learning Recommendation Model on NVIDIA GPUs 推荐系统帮助人在成倍增 ...

  10. 在OpenShift平台上验证NVIDIA DGX系统的分布式多节点自动驾驶AI训练

    在OpenShift平台上验证NVIDIA DGX系统的分布式多节点自动驾驶AI训练 自动驾驶汽车的深度神经网络(DNN)开发是一项艰巨的工作.本文验证了DGX多节点,多GPU,分布式训练在DXC机器 ...