zoukankan      html  css  js  c++  java
  • HDU

    Ladies and gentlemen, please sit up straight.
    Don't tilt your head. I'm serious.


    For n given strings S1,S2,,Sn, labelled from 1 to n, you should find the largest i (1in) such that there exists an integer j (1j<i) and Sj is not a substring of Si.

    A substring of a string Si is another string that occurs in Si

    . For example, ``ruiz" is a substring of ``ruizhang", and ``rzhang" is not a substring of ``ruizhang".
    InputThe first line contains an integer t (1t50) which is the number of test cases.
    For each test case, the first line is the positive integer n (1n500) and in the following n lines list are the strings S1,S2,,Sn.
    All strings are given in lower-case letters and strings are no longer than 2000 letters.OutputFor each test case, output the largest label you get. If it does not exist, output 1.Sample Input
    4
    5
    ab
    abc
    zabc
    abcd
    zabcd
    4
    you
    lovinyou
    aboutlovinyou
    allaboutlovinyou
    5
    de
    def
    abcd
    abcde
    abcdef
    3
    a
    ba
    ccc
    Sample Output
    Case #1: 4
    Case #2: -1
    Case #3: 4
    Case #4: 3

    思路:
    暑训的时候曾经写过这道题,不过我竟然忘了正解,实际上这题还是比较暴力的,由于题目要求的只是哪一个存在就行啦,所以在层层嵌套的情况下,不需要完全直接扫一编。
    于是,我们首先处理出,相邻的有哪些是满足字串的关系的,然后再做处理就行啦。
    代码
    #include<iostream>
    #include<cstring>
    using namespace std;
    char a[505][2048];
    int num[600];
    int main()
    {
        int T;
        scanf("%d",&T);
        int Ca = 0;
        while(T--){
            Ca++;
            int n;
            scanf("%d",&n);
            for(int i=1;i<=n;i++){
                scanf("%s",a[i]);
            }
            int ans=0;
            for(int i=1;i<n;i++){
                num[i]=strstr(a[i+1],a[i])?1:0;
            }
            for(int i=n;i>=1;i--){
                for(int j=1;j<i;j++){
                    if(num[j]){continue;}
                    if(!strstr(a[i],a[j])){
                        ans=i;
                        break;
                    }
                }
                if(ans){break;}
            }
            printf("Case #%d: ",Ca);
            if(ans){printf("%d
    ",ans);}
            else printf("-1
    ");
        }
    }
    
    
  • 相关阅读:
    python测试开发django-99.views视图中 locals() 函数使用
    python测试开发django-98.views视图函数中request参数详解
    jenkins学习18
    Assignment Problem的若干思考
    为博客园文章添加 “分享到Teams”按钮
    新书上市——Microsoft Teams 平台完全手册
    《Microsoft Teams 平台完全手册》第三章出炉
    Microsoft Teams平台手册公开邀请试读
    《Microsoft Teams平台完全手册》开工
    Teams 开发平台月报(2021-03)
  • 原文地址:https://www.cnblogs.com/ZGQblogs/p/9742896.html
Copyright © 2011-2022 走看看