博客
关于我
2021牛客寒假算法基础集训营3 F.匹配串
阅读量:169 次
发布时间:2019-02-28

本文共 1322 字,大约阅读时间需要 4 分钟。

题目描述

一个模式串指仅包含小写英文字母和至少一个’#‘的字符串,其中’#‘可以匹配一段任意长度的任意小写字母字符串。

一个匹配串指待匹配的只包含小写字母的字符串。
一个模式串和一个匹配串相匹配当且仅当把模式串里面的’#'全部分别替换成空或一段小写字母字符串后,两个串完全相同。
现在给出n个模式串,问有多少不同的匹配串与这些模式串全部相匹配。
如果答案有无穷多个,输出-1。

输入描述:

第一行一个正整数n。

接下来n行每行一个只包含小写字母和’#‘的模式串。(1≤n≤1e6)
保证输入模式串的长度总和不超过1e6且每个模式串至少包含一个’#’

输出描述:

一行一个整数代表答案。

如果答案有无穷多个,输出-1。

输入

2a#b#

输出

0

输入

2a#x#ca#c

输出

-1

思路

根据样例其实很容易推出结果只有0与-1两种情况,接下来就是判断的方法选取。我们可以观察到由于#可以更改为任意字符串,所以两个#之间的部分无论怎样都可以视作相等,我们只需要判断两个#之外的部分是否对应相等。在这个过程中,实际上每个字符串都在与第一个#与最后一个#之外的最长前后缀比较,我们可以选用数组对其进行记录。

代码

#include
using namespace std;const int mod=1e9+7;const int maxn=1e6+5;typedef long long ll;string s;bool flag;char head[maxn],tail[maxn];void judge(string a){ for(int i=0;i
=0;i--) { if(a[i]=='#') break; if(a[i]!=tail[cnt]) { if(tail[cnt]=='#') tail[cnt]=a[i]; else { flag=1; break; } } cnt++; }}int main(){ ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); int n; cin>>n; for(int i=0;i<=maxn;i++) { head[i]='#'; tail[i]='#'; } for(int i=1;i<=n;i++) { cin>>s; if(!flag) judge(s); } if(flag) cout<<"0"<<"\n"; else cout<<"-1"<<"\n"; return 0;}

转载地址:http://nhjj.baihongyu.com/

你可能感兴趣的文章
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>