首页 > 编程语言 >C++:[NWRRC2015] Concatenation(洛谷)P7050

C++:[NWRRC2015] Concatenation(洛谷)P7050

时间:2024-03-13 21:30:33浏览次数:20  
标签:NWRRC2015 洛谷 Gennady theap 单词 treap words P7050 he

题目描述

Famous programmer Gennady likes to create new words. One way to do it is to concatenate existing words. That means writing one word after another. For example, if he has words cat and dog, he would get a word catdog, that could mean something like the name of a creature with two heads: one cat head and one dog head.

Gennady is a bit bored of this way of creating new words, so he has invented another method. He takes a non-empty prefix of the first word, a non-empty suffix of the second word, and concatenates them. For example, if he has words tree and heap, he can get such words as treaptap, or theap. Who knows what they could mean?

Gennady chooses two words and wants to know how many different words he can create using his new method. Of course, being a famous programmer, he has already calculated the answer. Can you do the same?

输入格式

Two lines of the input file contain words chosen by Gennady. They have lengths between 11 and 100000100000 characters and consist of lowercase English letters only.

输出格式

Output one integer -- the number of different words Gennady can create out of words given in the input file.

题意翻译

题目描述

著名的程序员 Gennady 喜欢创造新单词。其中一种方法是连接现有单词。

举个例子:如果 Gennady 有 cat 和 dog 两个词,那么他会得到一个新词: catdog,这可能意味着带有两个头的生物的名字:一个猫头和一个狗头。

Gennady 觉得这种创建新单词的方式有点无聊,因此他发明了另一种方法:使用第一个单词的非空前缀,第二个单词的非空后缀,并将它们连接起来。例如,如果他有单词 tree 和 heap ,则可以得到诸如 treaptap 或 theap 之类的单词。

Gennady 选择了两个单词,并想知道他可以使用新方法创建多少个不同的单词。当然,作为著名的程序员,他已经计算出了答案。他突然想考考你,那么你能编写一个程序把答案计算出来吗?

输入格式

两行,每行有一个 Gennady 选择的单词 si​​ (1≤∣si​∣≤100000),si​ 仅由小写英文字母组成)。

输出格式

输出一个整数,这个整数表示 Gennady 可以从这两个给定的单词中创建不同单词的数量。

输入输出样例

输入 #1

cat
dog

输出 #1

9

输入 #2

tree
heap

输出 #2

14

说明/提示

Time limit: 2 s, Memory limit: 256 MB.

解析:

题目大意:

求两个单词能组成多少个新单词。

举例:

首先我们先拿两个单词来举例一下:

第一种是没有重复的情况:

dogdog 与 catcat。

它们能够组成的单词如下:

dcat , dat , dt , docat , doat , dot , dogcat , dogat , dogt dcat , dat , dt , docat , doat , dot , dogcat , dogat , dogt 

第二种是有重复的情况:

treetree 与 heapheap。

它们能够组成的单词如下:

theap , teap , tap , tp , trheap , treap , trap , trp , trehaep , treeap , treap , trep , treeheap , treeeap , treeap , treep theap , teap , tap , tp , trheap , treap , trap , trp , trehaep , treeap , treap , trep , treeheap , treeeap , treeap , treep 

我们能发现在这些新单词中有重复的,我们就将重复的删除掉。

剩下的单词: theap , teap , tap , tp , trheap , trap , trp , trehaep , treap , trep , treeheap , treeeap , treeap , treep theap , teap , tap , tp , trheap , trap , trp , trehaep , treap , trep , treeheap , treeeap , treeap , treep 

思路:

构成方法:用第一个单词的非空前缀,用第二个单词的非空后缀

那么我们可以用 ans 记录答案,再初始化设为两个单词长度之积,就是能创造的所有单词。

而前面我们已将两种情况都举例了一遍,就可以先减去所有重复单词,且重复单词就是两个单词相同的字母数量乘积,最后输出。

话不多说,上code:

#include <bits/stdc++.h>
using namespace std;
const int N=2e1+6;
string s1,s2;
long long a[N],b[N];
long long ans,x,y;
int main(){
	cin>>s1>>s2;
	x=s1.size(),y=s2.size();
	for(int i=1;i<x;i++)
		a[s1[i]-'a']++;
	for(int i=0;i<y-1;i++)
		b[s2[i]-'a']++;
	for(int i=0;i<26;i++)	
		ans+=a[i]*b[i];
	printf("%lld",x*y-ans);
	return 0;
}

记得点个赞哟!

标签:NWRRC2015,洛谷,Gennady,theap,单词,treap,words,P7050,he
From: https://blog.csdn.net/m0_72614289/article/details/136664738

相关文章

  • 洛谷 P2730 魔板 题解 DFS(广度优先搜索)
    题目背景在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板。这是一张有 8 个大小相同的格子的魔板:12348765题目描述我们知道魔板的每一个方格都有一种颜色。这 8 种颜色用前 8 个正整数来表示。可以用颜色的序列来表示一种魔板状态,规定从魔板的左......
  • 洛谷题单指南-二叉树-P4715 【深基16.例1】淘汰赛
    原题链接:https://www.luogu.com.cn/problem/P4715题意解读:计算亚军得主,注意能力值最高的肯定是冠军,但能力值第二高的不一定是亚军,因为有可能中途就遭遇冠军。解题思路:根据题意,两两比赛,一轮后再按顺序两两比赛,形如一棵二叉树,但解题其实用不到二叉树的数据结构可以看出,最后参与......
  • 每日一题 第一期 洛谷 铺地毯
    [NOIP2011提高组]铺地毯https://www.luogu.com.cn/problem/P1003题目描述为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯。一共有n......
  • 「杂题乱刷」洛谷 P2572
    先上AC代码:点击查看代码/*Tips:你数组开小了吗?你MLE了吗?你觉得是贪心,是不是该想想dp?一个小时没调出来,是不是该考虑换题?*/#include<bits/stdc++.h>usingnamespacestd;#definemapunordered_map#defineforl(i,a,b)for(registerlonglongi=a;i<=b;i++)#define......
  • 洛谷题单指南-线性表-P2234 [HNOI2002] 营业额统计
    原题链接:https://www.luogu.com.cn/problem/P2234题意解读:要计算每一天最小波动值的和,需要对每一天求最小波动值,再求和,如果暴力法,时间复杂度在1+2+3+......+32767≈5*10^8,可能会超时。解题思路:1、暴力法:由于本题测试数据比较水,实测暴力求解直接可以AC,由于没有技术含量,不做具体......
  • 「杂题乱刷」洛谷 P1708
    题目链接P1708解题思路解法一:考虑预处理,这部分可以直接打表。其他题解这部分讲的比较详细了,在此不再赘述。期望得分\(100\)分。解法二:考虑数位dp。这里采用记搜的写法。dfs(last,sum,maxsum,_1)分别表示还需要枚举几位数,目前枚举的数位和,可以枚举的最大数位和,是否均......
  • 洛谷题单指南-线性表-P4387 【深基15.习9】验证栈序列
    原题链接:https://www.luogu.com.cn/problem/P4387题意解读:判断一组序列入栈后,出栈序列的合法性。解题思路:数据长度100000,直接模拟堆栈的入栈和出栈即可遍历每一个入栈元素,依次入栈,每一个元素入栈后,比较栈顶元素和出栈序列第一个,如果相等,则出栈,持续进行比较、出栈直到不相等......
  • 洛谷题单指南-线性表-P1241 括号序列
    原题链接:https://www.luogu.com.cn/problem/P1241题意解读:括号配对问题,直观上是堆栈的应用。关键的匹配策略是读懂该句:考察它与它左侧离它最近的未匹配的的左括号。解题思路:本题所需核心数据结构是堆栈,由于要实现从栈顶找到第一个未匹配的左括号,所以堆栈中只存所有左括号。从......
  • 洛谷题单指南-线性表-P2058 [NOIP2016 普及组] 海港
    原题链接:https://www.luogu.com.cn/problem/P2058题意解读:计算24小时时间窗口内不同国家的数量,是队列的典型应用。解题思路:本题需要用到两个关键的数据结构:队列、数组队列用来保存24小时内到达的船的时间,数组用来保存24小时内每个国家有多少人每到一只船,需要把时间放入队列,如......
  • 洛谷题单指南-线性表-P1540 [NOIP2010 提高组] 机器翻译
    原题链接:https://www.luogu.com.cn/problem/P1540题意解读:本题模拟内存的调入调出,内存先入先出的特性就是队列。解题思路:本题需要两种数据结构:队列、数组队列用来模拟内存的操作,数组充当hash表用于判断单词在内存是否存在核心逻辑:对于每一个单词,如果内存不存在,查一次词典,再将......