您的位置:首页 > 其它

codeforces C. Hometask(贪心)

2016-07-20 19:45 267 查看
题意:在一串文字中有一些字符对(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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: