Bazinga HDU - 5510


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".
Input The 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 2000letters. Output For 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


题意:给定n个串,求最大的下表i使得在1~i-1中至少有一个串不是其子串



思路: 暴力O(T*n*n*(n+m)) 于是TLE

优化~用l代表对于当前i,所能抵达的l。如果s[l]不是s[i]的字串,break。 如果是,l++;


#include <stdio.h>
#include <string.h>
typedef long long ll;

using namespace std;

const int N=2e3+5;
char s[501][N];
int next[N];
int lena,lenb;
char a[N];
char b[N];
ll ans;

void set_naxt()
{
    int i=0,j=-1;
    next[0]=-1;
    while(i<lenb)
    {
        if(j==-1||b[i]==b[j])
        {
            i++; j++;
            next[i]=j;
        }
        else
        j=next[j];
    }
}

int kmp()
{
    int i=0,j=0;
    set_naxt();
    while(i<lena)
    {
        if(j==-1||a[i]==b[j])
        {
            i++;j++;
        }
        else
        j=next[j];
        if(j==lenb)
        {
            return 1;
        }
    }
    return 0;
}

int main(void){
    int T;
    int ks=1;
    scanf("%d",&T);
    while(T--){
        ans=-1;
        int m;
        scanf("%d",&m);
        register int i,l=1;
        bool flag=true;
        for(i=1;i<=m;i++)   scanf("%s",s[i]);
        for(i=1;i<=m;i++){
            while(l<i){
                strcpy(a,s[i]),strcpy(b,s[l]);
                lena=strlen(a),lenb=strlen(b);
                if(kmp()==1)    l++;
                else    {ans=i;break;}
            }
        }
        printf("Case #%d: %d\n",ks++,ans);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Element UI中,日期选择器可以使用v-model进行双向绑定,但如果你不想使用v-model,也可以通过其他方式实现单向绑定。例如,你可以使用@change事件来监听日期选择器的值变化,并将选中的日期保存在一个变量中。然后,你可以在需要的地方使用这个变量来获取选中的日期值。以下是一个示例代码: ```html <template> <div> <el-date-picker :value="selectedDate" @change="handleDateChange"></el-date-picker> <p>选中的日期:{{ selectedDate }}</p> </div> </template> <script> export default { data() { return { selectedDate: null }; }, methods: { handleDateChange(date) { this.selectedDate = date; } } }; </script> ``` 在上面的代码中,我们使用了一个变量`selectedDate`来保存选中的日期值。当日期选择器的值发生变化时,通过`@change`事件触发`handleDateChange`方法,将选中的日期赋值给`selectedDate`变量。然后,我们可以在页面上显示选中的日期值。 希望这个例子能够帮助你解决问题。如果还有其他疑问,请随时提问。 #### 引用[.reference_title] - *1* *2* [【Vue】vue的基本使用-双向数据绑定(v-model)并简单理解MVVM](https://blog.csdn.net/yy_bazinga/article/details/123515725)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [el-cascader级联选择器设置v-model后,变更v-model后界面不改变的解决方法](https://blog.csdn.net/ttphoon/article/details/108123108)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值