Codeforces Round #244 (Div. 2)D(字符串DP)

D. Match & Catch
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Police headquarter is monitoring signal on different frequency levels. They have got two suspiciously encoded strings s1 and s2 from two different frequencies as signals. They are suspecting that these two strings are from two different criminals and they are planning to do some evil task.

Now they are trying to find a common substring of minimum length between these two strings. The substring must occur only once in the first string, and also it must occur only once in the second string.

Given two strings s1 and s2 consist of lowercase Latin letters, find the smallest (by length) common substring p of both s1 and s2, where pis a unique substring in s1 and also in s2. See notes for formal definition of substring and uniqueness.

Input

The first line of input contains s1 and the second line contains s2 (1 ≤ |s1|, |s2| ≤ 5000). Both strings consist of lowercase Latin letters.

Output

Print the length of the smallest common unique substring of s1 and s2. If there are no common unique substrings of s1 and s2 print -1.

Sample test(s)
input
apple
pepperoni
output
2
input
lover
driver
output
1
input
bidhan
roy
output
-1
input
testsetses
teeptes
output
3
Note

Imagine we have string a = a1a2a3...a|a|, where |a| is the length of string a, and ai is the ith letter of the string.

We will call string alal + 1al + 2...ar (1 ≤ l ≤ r ≤ |a|) the substring [l, r] of the string a.

The substring [l, r] is unique in a if and only if there is no pair l1, r1 such that l1 ≠ l and the substring [l1, r1] is equal to the substring[l, r] in a.


题意:RT

思路:用dp1[i][j]表示s1中以i为起始,s2中以j为起始的最长匹配子串

           dp2[i][j]表示s1中以i为起始,s1中以j为起始的最长匹配串

           然后枚举s1的每个位置i,在s2中找出所有与i位置匹配的串

           在s1中找出所有(除了i位置)与i位置匹配的串

           然后分情况讨论即可~

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         #include 
        
          #include 
         
           #include 
          
            #include 
            using namespace std; typedef long long ll; const int MAXN = 5005; char s1[MAXN]; char s2[MAXN]; int dp1[MAXN][MAXN]; int dp2[MAXN][MAXN]; int main() { scanf("%s%s",s1,s2); int n1=strlen(s1); int n2=strlen(s2); for(int i=n1;i>=1;i--){ for(int j=n2;j>=1;j--){ if(s1[i-1]==s2[j-1])dp1[i][j]=dp1[i+1][j+1]+1; } } for(int i=n1;i>=1;i--){ for(int j=n1;j>=1;j--){ if(s1[i-1]==s1[j-1])dp2[i][j]=dp2[i+1][j+1]+1; } } int ans=200000; for(int i=1;i<=n1;i++){ int mx[2];mx[0]=mx[1]=0; int co=0; for(int j=1;j<=n2;j++){ if(dp1[i][j]==0)continue; co++; int v=dp1[i][j]; if(mx[1] 
            
              mx[0])swap(mx[0],mx[1]); } } if(mx[0]==mx[1])continue; int mx2=0; for(int j=1;j<=n1;j++){ if(j==i || dp2[i][j]==0)continue; mx2=max(mx2,dp2[i][j]); } if(mx2>=mx[0])continue; ans=min(ans,max(mx2,mx[1])+1); } if(ans==200000)printf("-1\n"); else printf("%d\n",ans); return 0; } 
             
           
          
         
       
      
      
     
     
    
    
   
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值