终曲
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1801 Accepted Submission(s): 542
Problem Description
最后的挑战终于到了!
站在yifenfei和MM面前的只剩下邪恶的大魔王lemon一人了!战胜他,yifenfei就能顺利救出MM。
Yifenfei和魔王lemon的挑战很简单:由lemon给出三个字符串,然后要yifenfei说出第一串的某个子串,要求该子串长度最小,并且同时包含第2个串和第3个串。
特别地,如果有多个这样的子串,则请输出字母序最小的一个。
站在yifenfei和MM面前的只剩下邪恶的大魔王lemon一人了!战胜他,yifenfei就能顺利救出MM。
Yifenfei和魔王lemon的挑战很简单:由lemon给出三个字符串,然后要yifenfei说出第一串的某个子串,要求该子串长度最小,并且同时包含第2个串和第3个串。
特别地,如果有多个这样的子串,则请输出字母序最小的一个。
Input
输入数据首先是一个整数C,表示测试数据有C组;
接着是C组数据,每组包含三行字符串,第一个字符串长度大于1小于100
后面两个串的长度大于1且小于10
接着是C组数据,每组包含三行字符串,第一个字符串长度大于1小于100
后面两个串的长度大于1且小于10
Output
请对应每组输入数据输出满足条件的最短子串;
如果没有,请输出 No
如果没有,请输出 No
Sample Input
2 abcd ab bc abc ab bd
Sample Output
abc No
数据范围很小,直接暴力第一个串的所有子串再查找就可以了
不知道为啥c++会ce g++就ac 求解
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
using namespace std;
struct node {
string ch;
int len;
}kk[11000];
int cm(node x,node y){//如果长度相同返回字典顺序小的,否则返回长度小的
if(x.len==y.len)return x.ch<y.ch;
return x.len<y.len;
}
int main() {
int t;
string ch,a,b;
cin>>t;
while(t--){
cin>>ch>>a>>b;
int ii=0,minn=99999;
for(int i=0;ch[i];i++){
for(int j=i;ch[j];j++){
string c;
int h=0;
for(int k=i;k<=j;k++){
c+=ch[k];
}
if(c.find(a)!=string::npos&&c.find(b)!=string::npos){//c中是否含有a,b
kk[ii].ch=c;
kk[ii].len=c.size();
minn=min(h,minn);
ii++;
break;//如果有满足的就不必继续,这个就是以ch[i]开头的最小子串
}
}
}
if(minn==99999){//如果一个满足的都没有
printf("No\n");
continue ;
}
sort(kk,kk+ii,cm);//对所有满足的串排序
cout<<kk[0].ch<<endl;
}
return 0;
}