2 条题解

  • 1
    @ 2026-4-9 16:52:13

    F02 字符串哈希

    // 字符串哈希 O(nm)
    #include<bits/stdc++.h>
    #define ull unsigned long long
    using namespace std;
    
    const int N=10010,B=131;
    int n;
    char s[N];
    ull h[N];
    
    ull get(char *s){ //s的哈希值
      int n=strlen(s+1);
      ull h=0;
      for(int i=1;i<=n;i++) h=h*B+s[i];
      return h;
    }
    int main(){
      cin>>n;
      for(int i=1; i<=n; i++){
        scanf("%s",s+1);
        h[i]=get(s);
      }
      
      sort(h+1,h+n+1);
      cout<<unique(h+1,h+n+1)-h-1;
      return 0;
    }
    
    • 0
      @ 2026-4-19 9:35:24
      #include<bits/stdc++.h>
      using namespace std;
      const int P = 131, mod = 998244353;
      int Hash(string s)
      {
          int v = 0;
          for (auto i : s) v = (v * P + i) % mod;
          return v;
      }
      int main()
      {
          int n; cin >> n;
          string s; set<int> st;
          for (int i = 1; i <= n; i++)
              cin >> s, st.insert(Hash(s));
          cout << st.size();
          return 0;
      }
      
      • 1

      信息

      ID
      11527
      时间
      1000ms
      内存
      128MiB
      难度
      6
      标签
      递交数
      102
      已通过
      29
      上传者