题意:

给出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. curl -O http://www.linux.com/hello.sh

    2.3:可以使用curl的内置option:-O(大写)保存网页中的文件要注意这里后面的url要具体到某个文件,不然抓不下来 # curl -O http://www.linux.com/hello. ...

  2. gitlab同步插件gitlab-mirrors报错<已解决,未找到原因>

    今天下午在使用gitlab-mirrors同步插件时,发现一直在报错 # ~/gitlab-mirrors/add_mirror.sh --git --project-name manifests - ...

  3. kubernetes 监控(14)

    一.Weave Scope 容器地图 创建 Kubernetes 集群并部署容器化应用只是第一步.一旦集群运行起来,我们需要确保一切正常,所有必要组件就位并各司其职,有足够的资源满足应用的需求.Kub ...

  4. OS_FLAG_GRP_DEPLETED

    178 * OS_FLAG_GRP_DEPLETED 系统没有剩余的空闲事件标志组,需要更改OS_CFG.H中 179 * 的事件标志组数目配置创建 标志组的时候返回这个错误 打印出错误代码后发现是1 ...

  5. 单片机编程时易错总结 20181015 项目:3060-A

    3060-A的调试过程中: 20181015 V1.30 A.遇到问题: RS232与LY3023的通信总是自己停止  主程序依旧执行 此版本进行如下修改: 1.RS232用的串口1关闭DMA传送   ...

  6. python 从2个文件中提取不相同的内容并输出到第三个文件中

    #-*- coding: UTF-8 -*- import re import sys import os   str1=[] str2=[] str_dump=[] fa=open("A. ...

  7. 重新整理 .net core 实践篇—————配置文件之环境配置[九]

    前言 在当今在互联网微服务比较适用的情况下,docker 可以说一个利器.每次我们打包docker的时候都是适用docker 的配置文件,那么配置文件里面会设置环境变量,这个时候需要我们的应用能够识别 ...

  8. .Net RabbitMQ实战指南——客户端开发

    开发中关键的Class和Interface有Channel.Connection.ConnectionFactory.Consumer等,与RabbitMQ相关的开发工作,基本上是围绕Connecti ...

  9. 女朋友看了也懂的Kafka(上篇)

    @ 目录 前言:为什么需要Kafka 1.初识kafka 1.1 消息队列的好处: 1.2 消息队列的两种模式 没有消息队列我们怎么做? 传统MQ怎么做? 这时候,Kafka就"闪亮登场&q ...

  10. GO学习-(15) Go语言基础之包

    Go语言基础之包 在工程化的Go语言开发项目中,Go语言的源码复用是建立在包(package)基础之上的.本文介绍了Go语言中如何定义包.如何导出包的内容及如何导入其他包. Go语言的包(packag ...