两下标指针解决覆盖子序列
小美找朋友
https://www.nowcoder.com/questionTerminal/58aa83aa37f14d11a51eb87b3813bbc6?answerType=1&f=discussion
一开始真的想得很复杂,联想到了覆盖子串问题用滑动窗口方法。看了评论区才恍然大悟,双指针去遍历就行了。
import java.io.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] params = br.readLine().trim().split(" "); int n = Integer.parseInt(params[0]); int m = Integer.parseInt(params[1]); String S = br.readLine().trim(); String T = br.readLine().trim(); int j = 0; long indexSum = 0; for(int i = 0; i < n; i++){ if(S.charAt(i) == T.charAt(j)){ j++; indexSum += i + 1; } } if(j == T.length()){ System.out.println("Yes"); System.out.println(indexSum); }else{ System.out.println("No"); } } }