链接:https://ac.nowcoder.com/acm/contest/1838/B
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld
题目描述
丁姐姐最近迷上了LCS(The longest common substring)!
今天她想找个其它东西玩,于是她找到了两个字符串A和B,现在她想知道它们尾头衔接形成的最长字符串,例如A = abc,B = bca从A的尾部开始,A串的bc与B串的bc头尾衔接。
输入描述:
输入数据包含多个测试样例,每个测试样例占两行,第一行是字符串A,第二行是字符串B,保证每个字符串的长度不超过1010。
输出描述:
A和B尾头衔接形成的最长字符串,对于每个测试实例,输出一行,若两个字符串不衔接,输出"NULL!"(包含引号)。
示例1
输入
复制
abc bca wad ad as asd wa aw wa wwa
输出
复制
bc ad as a "NULL!"
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <string>
#include <set>
#include <list>
#include <map>
#include <queue>
#include <stack>
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
string s1,s2;
while(cin>>s1>>s2){
int pos = -1,len=1;
for(int i=s1.length()-1;i>=0;i--,len++){
if(len>s2.length()) {
break;
}
string str1 = s1.substr(i);
string str2 = s2.substr(0,len);
//cout<<str1<<" "<<str2<<endl;
if(str1 == str2) {
pos = i;
continue;
}
}
if(pos == -1){
cout<<"\"NULL!\""<<endl;
}
else{
cout<<s1.substr(pos)<<endl;
}
}
return 0;
}