HDU 2572 终曲

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=2572

Problem Description
最后的挑战终于到了!
站在yifenfei和MM面前的只剩下邪恶的大魔王lemon一人了!战胜他,yifenfei就能顺利救出MM。
Yifenfei和魔王lemon的挑战很简单:由lemon给出三个字符串,然后要yifenfei说出第一串的某个子串,要求该子串长度最小,并且同时包含第2个串和第3个串。
特别地,如果有多个这样的子串,则请输出字母序最小的一个。
 
Input
输入数据首先是一个整数C,表示测试数据有C组;
接着是C组数据,每组包含三行字符串,第一个字符串长度大于1小于100
后面两个串的长度大于1且小于10
 
Output
请对应每组输入数据输出满足条件的最短子串;
如果没有,请输出 No
 
Sample Input
2
abcd
ab
bc
abc
ab
bd
 
Sample Output
abc
No
 
Hint:
题意:
中文。
题解:
直接做,自己用了string函数来判断。
代码:
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
string s,s1,s2,s3,str;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        cin>>s1>>s2>>s3;
        int n=s1.length();
        str=s1+"#";
        for(int i=0;i<=n;i++)
            for(int j=i+1;j<=n;j++)
            {
                s=s1.substr(i,j-i);//计算所有的s1的子串。
                if(s.find(s2)!=-1&&s.find(s3)!=-1)//看s1中是不是有s2,s3.
                {
                    if(s.length()<str.length())
                        str=s;
                    else if(s.length()==str.length())//当s,s1均满足条件的时候,寻找最小的子串。
                        if(s<str)
                            str=s;
                }
            }
        if(str.length()==n+1)
            cout<<"No"<<endl;
        else
            cout<<str<<endl;
    }
}
原文地址:https://www.cnblogs.com/TAT1122/p/5886293.html