codeforces C. Hometask(贪心)

题意:在一串文字中有一些字符对(2位)不允许出现,问最小修改数。输入时的字符对保证两个字符不同,且其中字符唯一

分析:一个字串只含有某个对中的字母,这个字符对才可能存在,要破坏这个字串,就在改变出现次数最少的那个字符,由此寻找符合题意的字串,并且求出修改次数,叠加即可

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 100005
char ch[maxn];
char s[2];
int n,k;
int main()
{
	scanf("%s",ch);
	//cout<<ch<<endl;
	int len=strlen(ch);
	scanf("%d",&k);	
		int i,ans=0;
	while(k--)
	{
		getchar();
		scanf("%s",s);
		//cout<<s<<endl;
		for(i=0;i<len;i++)
		{
			int l=0,r=0;
			while(ch[i]==s[0]||ch[i]==s[1])
			{
				//cout<<ch[i]<<endl;
				if(ch[i]==s[0]) l++;
				if(ch[i]==s[1]) r++;
				i++;
			}
			//cout<<l<<" "<<r<<endl;
			ans+=min(l,r);
			//ababcout<<ans<<endl;;
		}
	}
	printf("%d\n",ans);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值