package com.qingyun.service.compare;

import java.util.List;

/**
 * 判断两个文档是否具有可比性（LCS）
 */
public class DocumentStructureComparator {

    public static boolean isComparable(List<String> doc1Summaries, List<String> doc2Summaries) {
        int lcsLength = longestCommonSubsequence(doc1Summaries, doc2Summaries);
        int minLength = Math.min(doc1Summaries.size(), doc2Summaries.size());
        double similarity = (double) lcsLength / minLength;

        System.out.println("LCS 长度：" + lcsLength);
        System.out.println("最小页数：" + minLength);
        System.out.println("结构相似度：" + similarity);

        // 设置一个阈值，比如 0.6
        return similarity >= 0.6;
    }

    private static int longestCommonSubsequence(List<String> list1, List<String> list2) {
        int m = list1.size();
        int n = list2.size();

        // 使用一维数组优化空间
        int[] dp = new int[n + 1];

        for (int i = 1; i <= m; i++) {
            int prev = 0;
            for (int j = 1; j <= n; j++) {
                int current = dp[j];
                if (list1.get(i - 1).equals(list2.get(j - 1))) {
                    dp[j] = prev + 1;
                } else {
                    dp[j] = Math.max(dp[j], dp[j - 1]);
                }
                prev = current;
            }
        }
        return dp[n];
    }

}
