此篇属于比赛题解集合(CF && AcWing Race)
链接
欢迎留言讨论
B:传送门
解释
两种情况:
1.边缘,只需要一个节点相同
2.中间,只需要两个节点相同
可以证明(留个坑)中间单独节点是不能贡献的,一旦两个连起来,就是解
CODE
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
string a,b;
void solve()
{
if(a[0] == b[0])
{
puts("YES");
cout << a[0] << '*' << endl;
return ;
}
if(a.back() == b.back())
{
puts("YES");
cout << '*' << a.back() << endl;
return ;
}
for(int i = 0;i < a.size() - 1;i ++)
for(int j = 0;j < b.size() - 1;j ++)
if(a[i] == b[j] && a[i + 1] == b[j + 1])
{
puts("YES");
cout << '*' << a[i] << a[i + 1] << '*' << endl;
return ;
}
puts("NO");
return ;
}
int main()
{
int t;cin >> t;
while(t --)
{
cin >> a >> b;
solve();
}
}