做法
- 因为中间的字符都可以被‘#’代替,所以只要考虑开头和尾端,并且答案一定是不存在或者无穷多个
把每个字符串的前缀和后缀提取起来,然后比较依次比较每一位,如果其中有一位不同的话就一定不存在
代码
// Problem: 匹配串
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/9983/F
// Memory Limit: 524288 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp(aa,bb) make_pair(aa,bb)
#define _for(i,b) for(int i=(0);i<(b);i++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,b,a) for(int i=(b);i>=(a);i--)
#define mst(abc,bca) memset(abc,bca,sizeof abc)
#define X first
#define Y second
#define lowbit(a) (a&(-a))
#define debug(a) cout<<#a<<":"<<a<<"\n"
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef long double ld;
const int N=1000010;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const double eps=1e-6;
const double PI=acos(-1.0);
string s[N];
string p1[N],p2[N];
bool cmp(string s1,string s2){
return s1.length()<s2.length();
}
void solve(){
int n;cin>>n;
rep(i,1,n) cin>>s[i];
if(n==1){
cout<<"-1\n";
return;
}
rep(i,1,n){
for(char x:s[i]){
if(x=='#') break;
p1[i]+=x;
}
}
//rep(i,1,n) cout<<p1[i]<<"\n";
sort(p1+1,p1+1+n,cmp);
for(int i=1;i<n;i++){
for(int j=0;j<p1[i].length();j++){
if(p1[i][j]!=p1[i+1][j]){
cout<<"0\n";
return;
}
}
}
rep(i,1,n){
for(int j=s[i].size()-1;j>=0;j--){
if(s[i][j]=='#') break;
p2[i]+=s[i][j];
}
}
//rep(i,1,n) cout<<p2[i]<<"\n";
sort(p2+1,p2+1+n,cmp);
for(int i=1;i<n;i++){
for(int j=0;j<p2[i].length();j++){
if(p2[i][j]!=p2[i+1][j]){
cout<<"0\n";
return;
}
}
}
cout<<"-1\n";
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);
// int t;cin>>t;while(t--)
solve();
return 0;
}