The Dominator of Strings

1.题目
2.题解
3.注意点

The Dominator of Strings
Here you have a set of strings. A dominator is a string of the set dominating all strings else. The string S is dominated by T if S is a substring of T.
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 N indicating the size of the set.
Each of the following N lines describes a string of the set in lowercase.
The total length of strings in each case has the limit of 100000.
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

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    cin.sync_with_stdio(false);//使用这个语句可以使cin,cout的速度和scanf,printf差不多
    int num,N,maxss,maxlength;
    string s;
    vector<string> ss;
    bool flag = true;
    cin>>num;
    for(int i=0;i<num;i++)
    {
        cin>>N;
        maxlength = 0;  //???为什么-1不行
        flag = true;
        ss.resize(0);
        for(int j=0;j<N;j++)
        {
            cin>>s;
            if(s.size()>maxlength)  //maxlength赋初值为-1时不行因为size函数的返回值是无符号数,无符号数与有符号数进行比较时,会把有符号数转化为无符号数,并假设这两个数都是非负的来进行运算,-1会转化成4 294 967 295;
            {
                maxlength = s.size();
                maxss = j;
            }
            ss.push_back(s);
        }
        for(int j=0;j<N;j++)
        {
            if(ss[maxss].find(ss[j])==ss[maxss].npos)
            {
                flag = false;
                cout<<"No"<<endl;
                break;
            }
        }
        if(flag)
            cout<<ss[maxss]<<endl;
    }
    return 0;
}

注意事项

1.使用语句cin.sync_with_stdio(false);可以使cout,cin的输入速度和printf,scanf的速度差不多,不然会超时。
2.变量maxlength用于存储输入字符串的最大长度,但是不能赋初值为-1,**因为size函数的返回值为无符号数。在无符号数与有符号数比较的时候,会将有符号数转化为无符号数。有符号数转化为无符号数的时候,-1在内存中的储存方式不变,依然为11111111 11111111 11111111 11111111 ,按照无符号数的计算方法,最高位不再是符号位,转化为二进制为4 294 967 295,**会远远大于每个字符串的长度,将不能得到结果。关于无符号数与有符号数的计算与储存,推荐一篇博客 https://blog.csdn.net/JIEJINQUANIL/article/details/51789682
3.find函数可以用于判断某字符串S是否是字符串SS的子串。find函数在找不到子串的情况下会返回SS的nops值。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值