返回列表 發帖

e289. 美麗的彩帶(40%)

本帖最後由 李知易 於 2025-6-7 12:01 編輯

e289. 美麗的彩帶
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. string v[200005] = {};
  4. int main()
  5. {
  6.    
  7.     ios_base::sync_with_stdio(0);
  8.     cin.tie(0);
  9.     int n, m;
  10.     cin >> n >> m;//3 2 3 2 4 5 6

  11.     for(int i = 0; i < m; i++)
  12.     {
  13.         cin >> v[i];
  14.     }
  15.     unordered_map<string, int> t;
  16.     int sum = 0, dis = 0;

  17.     for(int i = 0; i < m; i++)
  18.     {
  19.         t[v[i]]++;
  20.         if(t[v[i]] == 1)
  21.             dis++;
  22.         if(i >= n)
  23.         {
  24.             t[v[i-n]]--;
  25.             if(t[v[i-n]] == 0)
  26.                 dis--;
  27.         }
  28.         if(dis == n)
  29.             sum++;

  30.     }
  31.     cout << sum;
  32.     return 0;
  33. }
複製代碼
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. string num[200005] = {};
  4. int main() {
  5.     cin.tie(0);
  6.     ios_base::sync_with_stdio(0);
  7.     unordered_map<string, int> mp;
  8.     int m, n;
  9.     cin >> m >> n;// 1 2 3 5 4 5 4
  10.     for(int i = 0; i < n; i++)
  11.         cin >> num[i];
  12.     int dis = 0, res = 0;
  13.     for(int i = 0; i < m; i++)
  14.     {
  15.         if(mp[num[i]] == 0)
  16.             dis++;
  17.         mp[num[i]]++;
  18.     }
  19.     res += (dis == m);// if(dis == m) res++;
  20.     for(int l = 0, r = m; r < n; l++, r++)//滑塊向右移動
  21.     {
  22.         mp[num[l]]--;
  23.         if(mp[num[l]] == 0)
  24.             dis--;
  25.         if(mp[num[r]] == 0)
  26.             dis++;
  27.         mp[num[r]]++;
  28.         res += (dis == m);
  29.     }
  30.      cout << res << endl;
  31.     return 0;
  32. }
複製代碼
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int m,n;
  4. int t=0;
  5. int dif=0;
  6. int com=0;
  7. unordered_map<string,int> mp;
  8. int main()
  9. {
  10.     cin.tie(0);
  11.     ios::sync_with_stdio(0);
  12.     cin>>m>>n;
  13.     string a[n];
  14.     for(int i=0;i<n;i++)
  15.         cin>>a[i];
  16.     for(int i=0;i<n+1;i++)
  17.     {
  18.         if(i<m)
  19.         {
  20.             if(mp[a[i]]!=0)
  21.             {
  22.                 mp[a[i]]+=100;
  23.                 com+=100;
  24.             }
  25.             else
  26.             {
  27.                 mp[a[i]]++;
  28.                 com++;
  29.             }
  30.         }
  31.         else if(i==n)
  32.         {
  33.             if(com==m)
  34.                 dif++;
  35.             if(mp[a[i-m]]>100)
  36.             {
  37.                 com-=100;
  38.                 mp[a[i-m]]-=100;
  39.             }
  40.             else
  41.             {
  42.                 com-=mp[a[i-m]];
  43.                 mp[a[i-m]]--;
  44.             }
  45.         }
  46.         else
  47.         {
  48.             if(com==m)
  49.                 dif++;
  50.             if(mp[a[i-m]]>100)
  51.             {
  52.                 com-=100;
  53.                 mp[a[i-m]]-=100;
  54.             }
  55.             else
  56.             {
  57.                 com-=mp[a[i-m]];
  58.                 mp[a[i-m]]--;
  59.             }
  60.             if(mp[a[i]]!=0)
  61.             {
  62.                 mp[a[i]]+=100;
  63.                 com+=100;
  64.             }
  65.             else
  66.             {
  67.                 mp[a[i]]++;
  68.                 com++;
  69.             }
  70.         }
  71.     }
  72.     cout<<dif;
  73.     return 0;
  74. }
複製代碼

TOP

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. unordered_map<string ,int> un;
  4. int main()
  5. {
  6.     cin.tie(0);
  7.     cin.sync_with_stdio(0);
  8.     int m,n;
  9.     cin>>m>>n;
  10.     string st[n];
  11.     for(int i=0;i<n;i++)
  12.     {
  13.         cin>>st[i];
  14.         un[st[i]]=0;
  15.     }
  16.     int l=0,r=0,sum=0;
  17.     while(r<n)
  18.     {
  19.         if(r-l==m)
  20.         {
  21.             un[st[l]]--;
  22.             l++;
  23.         }
  24.         else if(r-l==m-1)
  25.         {
  26.             if(un[st[r]]==0)
  27.             {
  28.                 un[st[r]]++;
  29.                 r++;
  30.                 sum++;
  31.             }
  32.             else if(un[st[r]]!=0)
  33.             {
  34.                 un[st[l]]--;
  35.                 l++;
  36.             }
  37.         }
  38.         else
  39.         {
  40.             if(un[st[r]]==0)
  41.             {
  42.                 un[st[r]]++;
  43.                 r++;
  44.             }
  45.             else if(un[st[r]]!=0)
  46.             {
  47.                 un[st[l]]--;
  48.                 l++;
  49.             }
  50.         }
  51.     }

  52.     cout<<sum;
  53.     return 0;
  54. }
複製代碼

TOP

返回列表