HDU - 6208 (ios::sync_with_stdio(false);加速cin cout)

Here you have a set of strings. A dominator is a string of the set dominating all strings else. The string SS is dominated by TT if SS is a substring of TT.

Input

The input contains several test cases and the first line provides the total number of cases. 
For each test case, the first line contains an integer NN indicating the size of the set. 
Each of the following NN lines describes a string of the set in lowercase. 
The total length of strings in each case has the limit of 100000100000. 
The limit is 30MB for the input file.

Output

For each test case, output a dominator if exist, or No if not.

Sample Input

3
10
you
better
worse
richer
poorer
sickness
health
death
faithfulness
youbemyweddedwifebetterworsericherpoorersicknesshealthtilldeathdouspartandpledgeyoumyfaithfulness
5
abc
cde
abcde
abcde
bcde
3
aaaaa
aaaab
aaaac

Sample Output

youbemyweddedwifebetterworsericherpoorersicknesshealthtilldeathdouspartandpledgeyoumyfaithfulness
abcde
No

大概题意: 给出n个串,是否有一个串是所有串的母串(包含其他所有串)。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=2e5+10;
struct node
{
    string s;
    int len;
     bool operator <(const node&a)const///重载<运算符
    {
        return len<a.len;
    }
};
int main()
{
    ios::sync_with_stdio(false);///加速cin和cout,这样就不会导致TLE了 很重要!!!
    int t;
    cin>>t;
    node a[maxn];
    while(t--)
    {
        int n;
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>a[i].s;
            a[i].len=a[i].s.length();
        }
        sort(a,a+n);
        int kase=n-1;
        while(kase>=1&&a[kase].s==a[kase-1].s)///判断是否有字符串相同的情况,如果没有的话则不执行。
            kase--;
        if(kase<=0)///如果kase==0即代表所有的字符串都相同。
        {
            cout<<a[0].s<<endl;
            continue;
        }
        if(a[kase].len==a[kase-1].len)///上面判断了字符串是否相同的情况,如果字符串不相同,长度还相等,则不可能出现母串的情况,所以输出no
        {
            cout<<"No"<<endl;
            continue;
        }
        ///上面把特殊情况排除之后,然后再在所有的串中用find函数遍历是否存在母串。
        int flag=1;
        for(int i=0;i<n-1;i++)
        {
            if(a[n-1].s.find(a[i].s)==-1)
            {
                flag=0;
                break;
            }
        }
        if(flag)
            cout<<a[n-1].s<<endl;
        else
            cout<<"No"<<endl;
    }
    return 0;
}

知识点: 

1、ios::sync_with_stdio(false); 用cin cout时加速输入输出,不会出现数据太多时出现Time Limit Exceeded的情况。具体原因:cin,cout之所以效率低,是因为先把要输出的东西存入缓冲区,再输出,导致效率降低,而这段语句可以来打消iostream的输入 输出缓存,可以节省许多时间。以后输入输出最好用scanf printf。

2、用string 存储字符串会方便很多,string 处理字符串更简单,相应知识点可看C++红书。

3、本题会出现字符串长度都相等的情况,然后分情况讨论(即存在特殊情况),这样写代码的时候可以先处理特殊情况,然后再在所有串中用find函数找出是否含有母串的情况。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值