最长公共子序列没法满足一种情况,就是主串是 xxx匹配匹配匹配 副串是 匹配匹配匹配xxx的这种情况 下面是ac代码 const int CHAR_SIZE = 26; int main() { int cn[2][CHAR_SIZE] = {0}; string s1, s2; cin >> s1 >> s2; int len = s1.size(); int j = 0, ans = 0; for (int i = 0; i < len; i++) { if(s1[i] == s2[j]){ j++; }else{ ans++; } cn[0][s1[i] - &#39;a&#39;]++; cn[1][s2[i] - &#39;a&#39;]++; } bool hasRes = true; for (int i = 0; i < CHAR_SIZE; i++) { if(cn[0][i] != cn[1][i]){ hasRes = false; break; } } DEBUG(hasRes ? ans : -1); return 0; }
点赞 1

相关推荐

牛客网
牛客企业服务