看了诸位大佬的改了好几个小时也还是死循环,人已经坏掉了,求求各位大佬救一下
#include<iostream>
#include<vector>
#include<string>
#include<set>
using namespace std;
set<string> s[20];
int solution(int m, int size, string& target)
{
vector<bool> dp(size + 1, false);
dp[0] = true;
int ans = 0;
for (int i = 1; i < dp.size(); i++)
{
for (int j = m > i ? i : m; j > 0; j--)
{
string temp = target.substr(i - j, j);
if (s[j].count(temp) && dp[i - j])
{
dp[i] = true;
ans = i;
break;
}
}
}
return ans;
}
int main()
{
string target;
string temp;
int m;
while (cin >> temp && temp != ".")
{
s[temp.size()].insert(temp);
m = max(m, int(temp.size()));
}
cin.tie(nullptr);
char ch;
while ((ch = getchar()) != EOF)
if(isalpha(ch))
target = target + temp;
int size = target.size();
int ans = solution(m, size, target);
cout << ans;
return 0;
}