codeforces 223B Two strings
来源:互联网 发布:消原唱软件 编辑:程序博客网 时间:2024/06/16 09:04
http://codeforces.com/problemset/problem/223/B
纯粹想锻炼一下自己的表达能力。
题意:给你两个字符串S,T。将S中所有的子序列(要求这些子序列和T串相同)染色,最后问你能否将S染全,即每个字符都会染到
思路:先从左往右匹配,求出S中当前字符最大可以匹配到T串的哪个位置,然后再反过来匹配一遍,求出S中当前字符最左可以匹配到T串的那个位置
比如
S: ababbaba
T: aba
那么S中第4、5个位置的'b'的最右能匹配到T中的位置都是上一个'b'(S中第二个字符)最右能匹配到的T串中的位置
最后判断一下每个位置能匹配到的最左和最右是否交叉就可以了
#include<cstdio>#include<cstring>const int maxn = 200010;int L[maxn],R[maxn];char s[maxn],t[maxn];int lastpos[maxn];int main(){scanf("%s%s",s,t);int ls=strlen(s);int lt=strlen(t);memset(lastpos,-1,sizeof(lastpos));int cur=0;L[0]=s[0]==t[0]?0:-1;for(int i=0;i<ls;i++) { L[i]=lastpos[s[i]-'a']; if(cur<lt && s[i]==t[cur]) { L[i]=cur; cur++; } lastpos[s[i]-'a']=L[i]; // printf("i=%d %d\n",i,L[i]);}memset(lastpos,-1,sizeof(lastpos));cur=lt-1;R[ls-1]=s[ls-1]==t[cur]?cur:-1;for(int i=ls-1;i>=0;i--){R[i]=lastpos[s[i]-'a'];if(cur>=0 && s[i]==t[cur]){R[i]=cur;cur--;}lastpos[s[i]-'a']=R[i];//printf("i=%d %d\n",i,R[i]);}for(int i=0;i<ls;i++){if(L[i]==-1 || R[i]==-1 || L[i]<R[i]) return printf("NO\n"),0;}printf("YES\n");return 0;}
- codeforces 223B Two strings
- codeforces-223B-Two Strings
- CF - 223 - B. Two Strings
- Codeforces Round #138 (Div. 1), problem: (B) Two Strings
- Codeforces Round #179 (Div. 2) B. Yaroslav and Two Strings
- CodeForces Round #179 (296B) - Yaroslav and Two Strings
- CodeForces 296B Yaroslav and Two Strings (容斥)
- Two strings CodeForces
- Codeforces Round #179 (Div. 2) B (codeforces 296b) Yaroslav and Two Strings
- CodeForces 224D - Two Strings
- CodeForces 762C Two strings
- Codeforces 296B Yaroslav and Two Strings dp+容斥(入门
- codeforces-559B-Equivalent Strings
- CodeForces 559B Equivalent Strings
- codeforces B. Mike and strings
- codeforces 559B Equivalent Strings
- CODEFORCES:B. Two Buttons
- [codeforces] 762C - Two strings 线段树
- 回调函数
- Seven habits of effective text editing
- Android EditView属性
- 对铁道部12306火车票购票网站的浅析
- 查看新浪博客的技术文章
- codeforces 223B Two strings
- uCOS3于s3c2440的移植
- 让ubuntu支持新浪微博
- C++运算符重载
- 分享一些网页设计的素材
- ifilter接口的实现
- Spring学习笔记(3)
- 15本经典C、C++、MFC、VC++教程与开发资料
- 用“XML解析开发包Jaxp”对XML进行Dom方式解析-readXml操作