• Bazinga 字符串HASH 这题不能裸HASH 要优化 毒瘤题


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

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

    A substring of a string SiSi is another string that occurs in SiSi. For example, ``ruiz" is a substring of ``ruizhang", and ``rzhang" is not a substring of ``ruizhang".

    InputThe first line contains an integer t (1t50)t (1≤t≤50)which is the number of test cases. 
    For each test case, the first line is the positive integer n (1n500)n (1≤n≤500) and in the following nn lines list are the strings S1,S2,,SnS1,S2,⋯,Sn. 
    All strings are given in lower-case letters and strings are no longer than 20002000 letters.
    OutputFor each test case, output the largest label you get. If it does not exist, output 1−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

    这题用hash预处理 然后用vector从上到下扫过去
    类似于维护当前未匹配的序列

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <queue>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <set>
     7 #include <iostream>
     8 #include <map>
     9 #include <stack>
    10 #include <string>
    11 #include <vector>
    12 #define  pi acos(-1.0)
    13 #define  eps 1e-6
    14 #define  fi first
    15 #define  se second
    16 #define  lson l,m,rt<<1
    17 #define  rson m+1,r,rt<<1|1
    18 #define  bug         printf("******
    ")
    19 #define  mem(a,b)    memset(a,b,sizeof(a))
    20 #define  fuck(x)     cout<<"["<<x<<"]"<<endl
    21 #define  sf(n)       scanf("%d", &n)
    22 #define  sff(a,b)    scanf("%d %d", &a, &b)
    23 #define  sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
    24 #define  sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
    25 #define  pf          printf
    26 #define  FRE(i,a,b)  for(i = a; i <= b; i++)
    27 #define  FREE(i,a,b) for(i = a; i >= b; i--)
    28 #define  FRL(i,a,b)  for(i = a; i < b; i++)
    29 #define  FRLL(i,a,b) for(i = a; i > b; i--)
    30 #define  FIN freopen("DATA.txt","r",stdin)
    31 #define  gcd(a,b) __gcd(a,b)
    32 #define  lowbit(x)   x&-x
    33 #pragma  comment (linker,"/STACK:102400000,102400000")
    34 using namespace std;
    35 typedef long long LL;
    36 typedef unsigned long long ULL;
    37 const int INF = 0x7fffffff;
    38 const int maxn = 5e4 + 10;
    39 ULL HASH[505], seed = 133331, p[2010], mp[505][2010];
    40 char  s2[2010],a[505][2020];
    41 int t, n;
    42 void init() {
    43     p[0] = 1;
    44     for (int i = 1 ; i <= 2010 ; i++)
    45         p[i] = p[i - 1] * seed;
    46 }
    47 int check(int i, int j) {
    48     int len1 = strlen(a[i]), len2 = strlen(a[j]);
    49     for (int k = 0 ; k <= len1 - len2 ; k++){
    50         if (mp[i][k + len2] - mp[i][k]*p[len2] == HASH[j]) return 1;
    51     }
    52     return 0;
    53 }
    54 int  main() {
    55     init();
    56     int cas=1;
    57     sf(t);
    58     while(t--) {
    59         sf(n);
    60         for (int i = 1 ; i <= n ; i++) {
    61             scanf("%s", a[i]);
    62             int len = strlen(a[i]);
    63             int top = 0;
    64             mp[i][0] = 0;
    65             for (int j = 0 ; j < len ; j++) {
    66                 s2[top++] = a[i][j];
    67                 mp[i][top] = mp[i][top - 1] * seed + a[i][j];
    68             }
    69             HASH[i] = mp[i][top];
    70         }
    71         int ans=-1;
    72         vector<int>s;
    73         s.clear();
    74         s.push_back(1);
    75         for (int i=2 ;i<=n ;i++) {
    76             while(s.size()) {
    77                 if ( check(i,s[s.size()-1]) ) s.pop_back();
    78                 else break;
    79             }
    80             s.push_back(i);
    81             if (s.size()>1) ans=i;
    82         }
    83         printf("Case #%d: %d
    ",cas++,ans);
    84       }
    85     return 0;
    86 }


  • 相关阅读:
    vue-cil和webpack中本地静态图片的路径问题解决方案
    vue-cil 中的配置分析
    webpack中mainifest.js vendor.js app.js 三者的区别
    css 中可以继承的属性
    有关正则表达式的Js方法(replace)
    css 常用的几种垂直居中(包括图片)
    如何在Vue中建立全局引用或者全局命令
    删除数组中多个不连续的数组元素的正确姿势
    数据库
    代码片段
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9427572.html
Copyright © 2020-2023  润新知