题意:

给出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;
}

最新文章

  1. [转]svn常用命令
  2. mysql查询今天,昨天,近7天,近30天,本月,上一月数据的方法(摘录)
  3. SharpGL学习笔记(十六) 多重纹理映射
  4. 从QQ网站中提取的纯JS省市区三级联动
  5. DNS协议 实践
  6. BZOJ 3170 松鼠聚会(XY坐标)
  7. CY7C68013A的一点总结
  8. iOS利用单例实现不同界面间的数据传输
  9. android 44 SQLiteOpenHelper
  10. 笔试题&amp;amp;面试题:输入一个维度,逆时针打印出一个指定矩阵
  11. 使用Heartbeat实现双机热备
  12. python 学习 [day8]class成员
  13. python编程总结
  14. 热部署环境下,dubbo序列化的bug和优化
  15. javascript初识
  16. .NET Core中的路由约束
  17. Saiku数据库迁移H2迁移到Mysql(二十二)
  18. js代码跑马灯效果-----轮播图字效果!
  19. DNN网络(一)
  20. 聊聊ReentrantLock的内部实现

热门文章

  1. 基于LNMP架构搭建wordpress个人博客
  2. k8s用 ConfigMap 管理配置(13)
  3. JavaEE 前后端分离以及优缺点
  4. yml配置从nacos配置中心取数据(单个或多个)
  5. VMware vSphere 7.0 Update 2 发布 - 数据中心虚拟化和 Kubernetes 云原生应用引擎
  6. GO语言的JSON01---序列化
  7. Supervisor 开始
  8. CVPR2018论文看点:基于度量学习分类与少镜头目标检测
  9. 在NVIDIA(CUDA,CUBLAS)和Intel MKL上快速实现BERT推理
  10. NVIDIA Turing Architecture架构设计(下)