2022.2.11(hash)

目录

CF1200E Compress Words

题目描述

输入格式

输出格式

题意翻译

输入输出样例


CF1200E Compress Words

题目描述

Amugae has a sentence consisting of nn words. He want to compress this sentence into one word. Amugae doesn't like repetitions, so when he merges two words into one word, he removes the longest prefix of the second word that coincides with a suffix of the first word. For example, he merges "sample" and "please" into "samplease".

Amugae will merge his sentence left to right (i.e. first merge the first two words, then merge the result with the third word and so on). Write a program that prints the compressed word after the merging process ends.

输入格式

The first line contains an integer nn ( 1 \le n \le 10^51≤n≤105 ), the number of the words in Amugae's sentence.

The second line contains nn words separated by single space. Each words is non-empty and consists of uppercase and lowercase English letters and digits ('A', 'B', ..., 'Z', 'a', 'b', ..., 'z', '0', '1', ..., '9'). The total length of the words does not exceed 10^6106 .

输出格式

In the only line output the compressed word after the merging process ends as described in the problem.

题意翻译

Amugae有n个单词,他想把这个n个单词变成一个句子,具体来说就是从左到右依次把两个单词合并成一个单词.合并两个单词的时候,要找到最大的i(i\ge 0)i(i≥0),满足第一个单词的长度为ii的后缀和第二个单词长度为ii的前缀相等,然后把第二个单词第ii位以后的部分接到第一个单词后面.输出最后那个单词

输入输出样例

输入 #1复制

5
I want to order pizza

输出 #1复制

Iwantorderpizza

输入 #2复制

5
sample please ease in out

输出 #2复制

sampleaseinout

题意

给你n个字符串,相同的地方可以拼接在一起,问你最后能拼成什么东西

思路 

1、找第一个串的后缀和第二个串的前缀 ,找到他们最长的公共部分拼在一起

2、所以就可以用kmp的next数组

3、但传统的KMP都是只求一个字符串的前后缀,本题却求的是分别两个字符串的前缀和后缀的最长相等长度

4、将已合并的字符串接到新的待合并字符串的后面,把两个字符串合并成了一个,而且也正好按顺序待合并前缀在前面已合并字符串后缀在后面

5、为了防止时间复杂度过长(会超时),截取已合并字符串和待合并字符串两者中较短的长度

6、并且在待合并字符串和已合并字符串之间还要加一个永远不会输入的字符进行阻隔

代码实现 

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 1000010 
char s[N],a[N];
int next[N],len;
int main()
{
	int n;
	cin>>n;
	cin>>s+1;
	len=strlen(s+1);
	for(int i=2;i<=n;i++)
	{
	    cin>>a+1;
	   	int lens=len,lena=strlen(a+1),cnt=lena;
	   	int minlen=min(lens,lena);
	   	a[++cnt]='#';
	   	for(int j=1;j<=minlen;j++)
	   	   a[++cnt]=s[lens-minlen+j];
	   	next[0]=next[1]=0;     
	   	int j=0;
	   	for(int k=2;k<=cnt;k++)
		{  
	   	 	while(j>0&&a[k]!=a[j+1]) j=next[j];
			if(a[k]==a[j+1]) j++;
			next[k]=j;		
		}
		for(int j=next[cnt]+1;j<=lena;j++) 
		   s[++len]=a[j];	 
	}
	cout<<s+1;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在VS 2022中配置OpenCV的contrib模块时,你需要设置OPENCV_EXTRA_MODULES_PATH变量为opencv_contrib源码的modules文件夹路径。如果这个变量为空,那说明你只安装了基础版的OpenCV,不包括contrib模块。 至于在VS 2022中配置OpenCV的步骤,你可以按照以下方法进行操作: 1. 下载OpenCV的源码,并解压到你的电脑上。 2. 打开VS 2022,创建一个新的项目。 3. 在项目中,右键点击解决方案资源管理器中的项目名称,选择“属性”。 4. 在属性窗口中,选择“VC++目录”选项。 5. 在“包含目录”一栏中,添加OpenCV的include文件夹路径,例如:opencv-windows\install\include。 6. 在“库目录”一栏中,添加OpenCV的lib文件夹路径,例如:opencv-windows\install\x64\vc17\lib。 7. 在“链接器” -> “输入”一栏中,添加需要的库文件。如果你选择了opencv_world配置,只需要添加以下四个lib文件:opencv_world470.lib、opencv_world470d.lib、opencv_img_hash470.lib、opencv_img_hash470d.lib。 8. 点击“应用”和“确定”保存设置。 回答完问题后,我还可以回答以下相关问题: 相关问题: 1. 如何在VS 2022中配置OpenCV的基础版? 2. 如何在VS 2022中下载和安装OpenCV的contrib模块? 3. 是否可以在VS 2022中同时配置OpenCV的基础版和contrib模块? 请注意,以上回答假设你已经正确安装了VS 2022和OpenCV,并且已经下载了OpenCV的源码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值