AC
2025-11-05 19:18:00
发布于:浙江
3阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
//统计字符串A中子串出现次数
int countOccurrences(const string &A, const string &pattern)
{
int count = 0;
int pos = 0;
while ((pos = A.find(pattern, pos)) != string::npos)
{
count++;
pos++;
}
return count;
}
string A,B;
int n;
int main()
{
cin >> A >> B >> n;
int maxLen = 0;
int low = 1, high = B.size();
//立即启动二分查找!
while (low <= high)
{
int mid = low + (high - low) / 2;
string prefix = B.substr(0, mid);
int occurrences = countOccurrences(A, prefix);
if (occurrences >= n)
{
maxLen = mid;
low = mid + 1;
}
else
high = mid - 1;
}
if (maxLen == 0)
cout << "IMPOSSIBLE" << endl;
else
cout << B.substr(0, maxLen) << endl;
return 0;
}
这里空空如也







有帮助,赞一个