Bazinga HDU - 5510(字符串匹配)

    技术2026-02-26  9

    思路: 1.直接暴力1e8会超时 2.不难发现如果s[i]是s[i+1]的子串,如果s[i]不是一个字符串的子串,那么s[i+1]一定不是该字符串的子串。

    #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; char s[510][2010]; int st[510]; int main() { int t; scanf("%d",&t); for(int h=1;h<=t;h++) { int n; memset(st,0,sizeof st); scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%s",s[i]); } for(int i=1;i<n;i++) { if(strstr(s[i+1],s[i])) st[i]=1; } int ans=-1; for(int i=n;i>=1;i--) { for(int j=1;j<i;j++) { if(st[j]) continue; if(!(strstr(s[i],s[j]))) { ans=i; break; } } if(ans!=-1) break;//不能少 } printf("Case #%d: %d\n",h,ans); } }
    Processed: 0.009, SQL: 9