题意:

给出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. mate-notification-daemon stopping, Mate notifications timeout

    mate-notification-daemon stopping, Mate notifications timeout Ask Question Asked 9 days ago Viewed 1 ...

  2. 第14讲 | HTTP协议:看个新闻原来这么麻烦

    第14讲 | HTTP协议:看个新闻原来这么麻烦 http://www.163.com 是个 URL,叫作统一资源定位符. 之所以叫统一,是因为它是有格式的.HTTP 称为协议,www.163.com ...

  3. SpringBoot 实现整合log4j2日志

    关于日志级别 共有8个级别,按照从低到高为:All < Trace < Debug < Info < Warn < Error < Fatal < OFF. ...

  4. 【RMAN】使用RMAN备份将数据库不完全恢复到指定时间点

    RMAN作为Oracle强大的备份恢复工具,可以协助我们恢复数据库到指定时间点,这便是Oracle不完全恢复的一种体现,通过这种方法可以找回我们曾经丢失的数据.这里以找回误TRUNCATE表数据为例给 ...

  5. 定位服务API案例

    定位服务API案例 要使用定位服务API,需要确保设备已经下载并安装了HMS Core服务组件,并将Location Kit的SDK集成到项目中. 指定应用权限 Android提供了两种位置权限: A ...

  6. CodeGen字段循环Field Loop

    CodeGen字段循环Field Loop 字段循环是一个模板文件构造,它允许迭代CodeGen拥有的有关字段的集合.这些字段定义可以来自以下两个位置之一: •如果基于从存储库结构中获取的信息生成代码 ...

  7. 使用NVIDIA GRID vPC支持视频会议和算力工具

    随着2020年的发展,远程工作解决方案已成为许多人的新常态.企业正在寻找行之有效的解决方案,如虚拟桌面基础设施(VDI),以使他们的团队能够在任何地方安全地工作.然而,最新的算力和视频会议应用程序需要 ...

  8. python_request 使用jsonpath取值结果,进行接口关联

    一.jsonpath的安装 pip   install  jsonpath 二.使用举例 import jsonpath d1={"token":"hjshdsjhdsj ...

  9. Python_selenium页面元素整合设计经验

  10. 6, java数据结构和算法: 栈的应用, 逆波兰计算器, 中缀表达式--> 后缀表达式

    直接上代码: public class PolandCalculator { //栈的应用:波兰计算器: 即: 输入一个字符串,来计算结果, 比如 1+((2+3)×4)-5 结果为16 public ...