Luogu P3405

Luogu P3405

At a glance, I know I need to use map again, and I quickly build the code in my mind

#include<bits/stdc++.h>
using namespace std;
map<string,int>mp;
int main()
{
    
    
	int n,ans=0;
	string a,b,c[200005];
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
    
    
		cin>>a>>b;
		c[i]=a.substr(0,2);
		mp[b]++;
	}
	for(int i=1;i<=n;i++)
	{
    
    
		if(mp[c[i]]==1)
		{
    
    
			ans++;
		}
	}
	printf("%d",ans);
	return 0;
}

Review questions, type code, submit, all in one go!
The result...
10 points! (Actually, I didn't really think about it. After the sample is over, submit it and try my luck.)
But what's wrong?
Previously, I considered the city name and the state separately, which may cause me to be the same as myself, which makes ans bigger!
After discovering this, the algorithm was adjusted for the first time

#include<bits/stdc++.h>
using namespace std;
map<string,int>mp;
int main()
{
    
    
	int n,ans=0;
	string a,b,c[200005];
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
    
    
		cin>>a>>b;
		a=a.substr(0,2);
		if(a!=b)
		{
    
    
			ans+=mp[a+b];//把mp中a+b的一项加上
			mp[b+a]++;//同时把mp中它的顺序反过来(符合题意)
		}
	}
	printf("%d",ans);
	return 0;
}

Perfect AC!

Guess you like

Origin blog.csdn.net/weixin_50624971/article/details/113355328