標題:
e289. 美麗的彩帶(40%)
[打印本頁]
作者:
李知易
時間:
昨天 03:49
標題:
e289. 美麗的彩帶(40%)
本帖最後由 李知易 於 2025-6-7 12:01 編輯
e289. 美麗的彩帶
[attach]21248[/attach][attach]21248[/attach]
#include<bits/stdc++.h>
using namespace std;
string v[200005] = {};
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;//3 2 3 2 4 5 6
for(int i = 0; i < m; i++)
{
cin >> v[i];
}
unordered_map<string, int> t;
int sum = 0, dis = 0;
for(int i = 0; i < m; i++)
{
t[v[i]]++;
if(t[v[i]] == 1)
dis++;
if(i >= n)
{
t[v[i-n]]--;
if(t[v[i-n]] == 0)
dis--;
}
if(dis == n)
sum++;
}
cout << sum;
return 0;
}
複製代碼
#include<bits/stdc++.h>
using namespace std;
string num[200005] = {};
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
unordered_map<string, int> mp;
int m, n;
cin >> m >> n;// 1 2 3 5 4 5 4
for(int i = 0; i < n; i++)
cin >> num[i];
int dis = 0, res = 0;
for(int i = 0; i < m; i++)
{
if(mp[num[i]] == 0)
dis++;
mp[num[i]]++;
}
res += (dis == m);// if(dis == m) res++;
for(int l = 0, r = m; r < n; l++, r++)//滑塊向右移動
{
mp[num[l]]--;
if(mp[num[l]] == 0)
dis--;
if(mp[num[r]] == 0)
dis++;
mp[num[r]]++;
res += (dis == m);
}
cout << res << endl;
return 0;
}
複製代碼
作者:
張紘鳴
時間:
昨天 12:02
#include<bits/stdc++.h>
using namespace std;
int m,n;
int t=0;
int dif=0;
int com=0;
unordered_map<string,int> mp;
int main()
{
cin.tie(0);
ios::sync_with_stdio(0);
cin>>m>>n;
string a[n];
for(int i=0;i<n;i++)
cin>>a[i];
for(int i=0;i<n+1;i++)
{
if(i<m)
{
if(mp[a[i]]!=0)
{
mp[a[i]]+=100;
com+=100;
}
else
{
mp[a[i]]++;
com++;
}
}
else if(i==n)
{
if(com==m)
dif++;
if(mp[a[i-m]]>100)
{
com-=100;
mp[a[i-m]]-=100;
}
else
{
com-=mp[a[i-m]];
mp[a[i-m]]--;
}
}
else
{
if(com==m)
dif++;
if(mp[a[i-m]]>100)
{
com-=100;
mp[a[i-m]]-=100;
}
else
{
com-=mp[a[i-m]];
mp[a[i-m]]--;
}
if(mp[a[i]]!=0)
{
mp[a[i]]+=100;
com+=100;
}
else
{
mp[a[i]]++;
com++;
}
}
}
cout<<dif;
return 0;
}
複製代碼
作者:
洪承廷
時間:
昨天 12:07
#include<bits/stdc++.h>
using namespace std;
unordered_map<string ,int> un;
int main()
{
cin.tie(0);
cin.sync_with_stdio(0);
int m,n;
cin>>m>>n;
string st[n];
for(int i=0;i<n;i++)
{
cin>>st[i];
un[st[i]]=0;
}
int l=0,r=0,sum=0;
while(r<n)
{
if(r-l==m)
{
un[st[l]]--;
l++;
}
else if(r-l==m-1)
{
if(un[st[r]]==0)
{
un[st[r]]++;
r++;
sum++;
}
else if(un[st[r]]!=0)
{
un[st[l]]--;
l++;
}
}
else
{
if(un[st[r]]==0)
{
un[st[r]]++;
r++;
}
else if(un[st[r]]!=0)
{
un[st[l]]--;
l++;
}
}
}
cout<<sum;
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2