#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MAX_SIZE = 1e5+10;
char text[MAX_SIZE];
/**
Title: 白濑肆×字符串+DP——果然是字符串处理什么的好讨厌啊尤其是换行符的处理看来不用CIN不行了呢虽然效率低人家好伤心的还有就是DP的转移真心不会啊水到家了怎么办!
Problem:CodeForces 155C Hometask
Little Exp.: cin输入单个字符(char)的时候自动无视掉换行符,而scanf和getchar都不行。
pre-Knowledge: None.Only ur intellgence.
Knowledge Point: DP
Thx to: severin&&gzm提供的AC代码...刚开始打成serverin...弄错别人的id是很失礼的啊。。金魂(啊哈哈君你好么喂
Reference: ↑,但是其本人未公开发布,所以恕不提供啦:)
Thought:
看来我果然想复杂了。
【之前的错误思路】:
根据上一次转移遗留下来的字母进行转移,看看是删除左边还是删除右边,数出合法状态数。
很明显,这D个屁P,这明显就是暴搜思路。
更不用说暴搜还没过。
【正确思路】:
每次读入一对儿禁止出现的字符S[0],S[1],注意题意中说每个字母最多在一个pair中出现。
对于读入的字符串text[i],划掉text[n..k]即可。text[n..k]有如下规定:
text[n..k]的性质是对于i : n <= i <= k
有 text[i] == S[0]或text[i] == S[1];
也就是说text[n]到text[k](包括n和k)全是不可以出现的字符。
统计一下如果是去掉s[0]【或者】去掉s[1]使得text[n..k]变成合法的,需要划掉多少次。
sum加上小的那个次数即可。
比如
aaaaabcddddd
2
ad
bc
那么,在text[0..4]的时候我们需要划掉a 5次,划掉d 0 次,此时sum = sum + 0 = 0;( sum初始化为0)
text[5..6]的时候需要划掉b 1次。 c 1次,此时sum = sum + 1 = 1
text[7..末尾]的时候需要划掉d 5次,a 0次,此时sum = sum + 0 = 1;
结果就是1咯
【为什么?】(高能警告:本人数学白痴,自诩这种证明方式是正确的,跪求板砖或指出证明出错的地方)
对于原始串text,以最小次数去掉其中第一对禁止字符s[]之后,剩下的串是合法的。得到串text'
对于串text',重复以最小次数去掉其中第i对禁止字符s[],得到的新串仍然是合法的
只不过我们没有改变text的内容(亦即没有真的去掉其中的字母),而是记录下了最小次数累加而已。
不用改变text内容的原因是,题意中说每个字母在禁止出现的字母对中最多出现一次。
所以禁止字母对s[],s'[]中的内容不可能相同,彼此独立。
以上,反守为攻の白濑肆
*/
int main()
{
char a = 0;
char b = 0;
int K;
int ans = 0;
gets(text);
int lent = strlen(text);
scanf("%d\n",&K);
for(int k = 0 ; k < K ; k++)
{
cin >> a;
cin >> b;
for(int i = 0 ; i < lent ; i++)
{
int left = 0 ,right = 0;
while( text[i] == a || text[i] == b )
{
if( text[i] == a )
left++;
if( text[i] == b )
right++;
i++;
}
ans += min(left,right);
}
}
printf("%d\n",ans);
return 0;
}
相关推荐
codeforces 155C dp
//R 取出最少的字符 使得里面不包含给定的字符对和逆序字符对 /*思路:cf题目一般难想 但是代码简洁 简单统计一下就好啦 我一定要想办法拿下C题 */ #include #include #include #include #include #include #include #include #include #include #include #include using namespa
HDU 4357 字符交换位置无数次变成另一个字符串-YY-(神题
题意:有两个字符串,第一个字符串的任意两个字符可以交换位置,交换位置后的两个字符要+1,也就是a变成b,z变成a,问第一个字符串能否最终变成第二个字符串。 分析: 神题! 长度为2的和长度大于2的分开讨论,但是都是直接yy大法。 长度为2的好想,自己想。长度大于2的,这篇博客写得很详细点击打开链接,谢谢博主。 代码: #include #include using namespace
CodeForces 23A You're Given a String...(字符串处理)
Description You’re given a string of lower-case Latin letters. Your task is to find the length of its longest substring that can be met in the string at least twice. These occurrences can overlap (see
【CodeForces - 155C】Hometask (字符串,思维,贪心,熟悉句式)(总结)
题干: Sergey attends lessons of the N-ish language. Each lesson he receives a hometask. This time the task is to translate some sentence to the N-ish language. Sentences of the N-ish language can be re...
【POJ1077】Eight 八数码问题,解题报告+思路+代码
#include #include #include #define INPUT using namespace std; /** Problem : poj1077,hdu1043,经典的八数码问题。 知识点: BFS + HASH + 打表 + 父亲节点记录 境界:3 - BFS+HASH+打表 A了两天!!! 记得拿启发式,双向BFS重新写
【HDU2222】 0.6%达成 模板题 果然无聊的时候A题才是最正确的选择啊因为你就会更加无聊
#include #include #include #include #include using namespace std; /** Reference:http://www.cnblogs.com/procedure2012/archive/2012/01/29/2331460.html Thanks to: lpp学长给的模板和讲义 Pre-Knowle
【ZOJ3587】Marlon's String——白四爷×KMP 白濑肆の算法完全解读KMP篇 KMP来袭第二弹前缀什么的果然最讨厌了!【1.0%达成!】
#include #include #include #include /** Title: 【ZOJ3587】Marlon's String——白四爷×KMP 白濑肆の算法完全解读KMP篇 KMP来袭第二弹前缀什么的果然最讨厌了!【1.0%达成!】 Problem: ZOJ3587 Marlon's String Knowledge Point : KMP算法の深
【HDU4313】Matrix 多校 解题报告+AC代码+思路+算法正确性证明,此为Kruskal贪心恶心版本,非自虐倾向慎入!建议想找解题报告的童鞋看简单版本的,这个我写给自己【目标达成 0.2%】
#include #include #include #include using namespace std; /** 【警告】这个恶心版本是我写给自己看的,关于解题报告,还是请移步简单版本吧,就在这篇文章的前一篇。 c0de4fun声明:本人未给出测试数据、未声明一次AC的题均为参考解题报告自己写的。 对那些无私贡献的大神真诚
【ACM模板】HASH表(无删除功能,添查)
#include #include #include #include #include #include using namespace std; const int HASH_SIZE = 1000000; const int HASH_KEY = 17; int next[HASH_KEY][HASH_SIZE]; int hash[HASH_SIZE]; int origin_
【0.9%】SPOJ7758 Grwoing Strings 解题报告 + AC代码 + 思路 + AC自动机简短总结
#include #include #include #include #include using namespace std; const int MAX_SIZE = 1000100; /** 【0.9%】SPOJ7758 Grwoing Strings 解题报告 + AC代码 + 思路 + AC自动机简短总结 ——果然智商捉急的话就要WA到死才能AC啊人生来不是给
【HDU3415】Max Sum of Max-K-sub-sequence,思路+解题报告+AC代码+自虐般疯狂吐槽【0.3%达成!】
#include #include #include #include using namespace std; const int MAX_SIZE = 100010 * 2; const int INFINITE = 99999999; int num[MAX_SIZE]; int sum[MAX_SIZE]; int q[MAX_SIZE]; int ind[MAX_SIZE]; i
【ZOJ3228】白濑肆の完美算法教室补番作,0.7%达成 白濑肆×AC自动机
#include #include #include #include #include using namespace std; /** 就是一个AC自动机 但是每个单词有两个状态,[0]是可以overlap,[1]是不能overlap时候的次数 我们用last数组记录下每个不能overlap的单词出现的位置,如果 当前位置 - last[word]
【UVA11205】The broken pedometer,思路+代码,可能是最不装逼最朴素最易懂效率也最差的代码。
#include #include #include #include #include using namespace std; /** Problem : UVA11205 - The broken pedometer Begin Time: 28th/Mar/2012 11:30 a.m. Finish Time: 29th/Mar/2012 2:14 a.
【UVa10167】 Birthday Cake,思路+代码+解题报告
#include #include #include #include // #define INPUT /** Problem: UVA10167 - Birthday Cake Begin Time:22nd/Mar/2012 5:00 p.m. End Time: 22nd/Mar/2012 7:35 p.m. Last Time: 2H 35M
【HDU4313】Matrix 多校 解题报告+AC代码+思路+算法正确性证明,此为Kruskal贪心简单版本,恶心版本稍后放出【目标达成 0.2%】
#include #include #include #include using namespace std; /** c0de4fun声明:本人未给出测试数据、未声明一次AC的题均为参考解题报告自己写的。 对那些无私贡献的大神真诚的致敬!本题的测试数据可以看最后面 Problem: HDU4313 - Matrix Refe
【HDU4313】 - Matrix - 树状DP Version 思路+解题报告+AC代码【0.4%达成】
#include #include #include #include #include using namespace std; /** Problem: HDU4313 - Matrix - DP Version【0.4%达成】 Copyright : 归我们学校集训队和本人所有,未经同意可以转载。 【但是胆敢用于培训的话,贵学校服务
【HDU2571】命运
#include #include #include #include using namespace std; const int MAXN = 100; const int MAXM = 1100; int maze[MAXN][MAXM]; int dp[MAXN][MAXM]; int solve(int n,int m,int N,int M) { //向下一次只能走一格。但是
【ZOJ3471】Most Powerful, 思路+源代码+解题报告(好久没写解题报告了……)【目标达成:0.1%】
/** Problem:ZOJ3471-Most Powerful. Reference:http://blog.csdn.net/magicnumber/article/details/6182891 Knowledge point:状态压缩DP Thought: 根据炮兵阵地(那是别的题)和这道题,还有一次多校的经验(就是那个类似拼图的那个题,给你
291




被折叠的 条评论
为什么被折叠?



