HDU 6208 && 2017 ACM/ICPC Asia Regional Qingdao Online1003

The Dominator of Strings

Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 497 Accepted Submission(s): 139

Problem Description
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

对于给定的字符串,求是否有一个字符串包括其他所有字符串,如果有就输出这个字符串,如果没有就输出NO

一直是队友在做这个题,它觉得KMP(然而他写错了,一直WA,),这个题描述的太模糊了,字符串的范围也不给,想不懂,最开始我觉得KMP时间复杂度也有点高了,没想到这个题数据比较水,直接用string.find就能过,我觉得可能是除了最长的那个字符串外,其他的字符串都比较短所以find可以解决

代码如下:

#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;
const int MAX_V = 100010;

string str[MAX_V];
int pos,n;
bool solve(){
    for(int i=1;i<=n;i++){
        if(str[pos].find(str[i]) == string::npos)//.find如果没找到返回的是string::npos这个常量,一般是-1
            return false;
    }

    return true;
}

int main(void){
    int t;
    ios::sync_with_stdio(false);
    cin >> t;
    while(t--){
        cin >> n;
        pos = -1;
        for(int i=1;i<=n;i++){
            cin >> str[i];
            if(pos == -1 || str[i].length() > str[pos].length())
                pos = i;
        }
        if(solve())
            cout << str[pos] << endl;
        else
            cout << "No" << endl;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值