AcWing 779. 最长公共字符串后缀
原题链接
困难
作者:
我要出去乱说
,
2021-01-20 02:17:18
,
所有人可见
,
阅读 547
#include <iostream>
using namespace std;
const int N = 200;
int n;
string str[N];
int main()
{
while (cin >> n, n) //当读到0的时候结束
{
int len = 210;
for (int i = 0; i < n; i ++ )
{
cin >> str[i];
if (len > str[i].size()) len = str[i].size(); //得到最小字符串的长度
}
while (len)
{
bool success = true;
for (int i = 1; i < n; i ++ )
{
bool is_same = true;
//关键在于j<=len,当后缀不同时len会减一重开while循环,知道len减小到相同后缀长度为止
for (int j = 1; j <= len; j ++ )
if (str[0][str[0].size() - j] != str[i][str[i].size() - j])
{
is_same = false;
break; //弹出最里层for循环
}
if (!is_same)
{
success = false;
break; //弹出最外层for循环
}
}
if (success) break; //弹出while循环
len -- ;
}
cout << str[0].substr(str[0].size() - len) << endl; //输出最长公共后缀
}
return 0;
}
请问一下if (str[0][str[0].size() - j] != str[i][str[i].size() - j]) 这个代码是啥意思