AcWing 778. 字符串最大跨距 - Java
原题链接
困难
作者:
KYCygni
,
2021-03-20 03:33:47
,
所有人可见
,
阅读 340
Java 代码
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main
{
public static void main(String[] args)
{
Scanner cin = new Scanner(System.in);
StringTokenizer st = new StringTokenizer(cin.nextLine(), ",");
String s = st.nextToken();
String s1 = st.nextToken();
String s2 = st.nextToken();
int len = s.length();
int len1 = s1.length();
int len2 = s2.length();
if (len < len1 || len < len2)
System.out.println (-1);
else
{
int l = -1;
for (int i = 0; i <= len - len1; i++)
{
if (s.substring(i, i+len1).compareTo(s1) == 0)
{
l = i + len1;
break;
}
}
int r = -1;
for (int j = len - len2; j >=0 ; j -- )
{
if (s.substring(j, j+len2).compareTo(s2) == 0)
{
r = j;
break;
}
}
if (l > -1 && r > -1 && r > l)
System.out.println (r - l);
else
System.out.println (-1);
}
}
}
abcd,ab,cd 这个就过不去了