Educational Codeforces Round 46 Problem A Codehorses T-shirts

Link

http://codeforces.com/contest/1000/problem/A

Sloution

At first, I enumerate every s[i] and compare with every t[j], trying to find the minimum cost to delete s[i]. When I find t[j] which matches condition, I add the cost of changing s[i] into t[j] into answer and delete both s[i] and t[j]. The result of this alogrihm is Wrong Answer.
Think anout this case:
4
L
M
M
S
If I match “L” with “M”, the cost will be 2.
But there exit a better way whilch matches “L” whth “S” and “M” with “M”. The cost is 1.
That means, my choice may damage some pairs and raise the total cost.
So I then focused on network flow.
I firstly set up node S and T.
Then add edges form S to every s[i], whose flow limits are 1 and costs are 0.
Then add edges from every t[j] to T, whose flow limits are 1 and costs are 0.
If s[i]’s length equals to t[j], I add an edge from i to n+j, whose flow limit is inf and cost is 1.
Then answer is the max-flow min-cost.
But the amount of edges is too large so that the result was Time Limit Exceeded.
During the last 5 minutes, I found the correct and my program was Accepted.
I enumerate k from 0 to 4, which means the cost to change s[i] to t[j].
then I enumerate i and j, if the cost of changing s[i] to t[j] equals to k, I change it.

Code

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <iostream>
#define maxn 1000 
using namespace std;
int n, ans, die[maxn];
string s[maxn], t[maxn];
int calc(int a, int b)
{
    int i, cnt=0;
    if(s[a].length()!=t[b].length())return 100;
    for(i=0;i<s[a].length();i++)cnt+=s[a][i]!=t[b][i];
    return cnt;
}
int main()
{
    int i, x, j,  cost, k;
    scanf("%d",&n);
    for(i=1;i<=n;i++)cin>>s[i];
    for(i=1;i<=n;i++)cin>>t[i];
    for(k=0;k<=4;k++)
        for(i=1;i<=n;i++)for(j=1;j<=n;j++)if(!die[i] and !die[n+j] and calc(i,j)==k)die[i]=die[n+j]=1, ans+=k;
    printf("%d",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/fsahfgsadhsakndas/article/details/80848114