CRB and String(HDUOJ--5414
2015-08-20 21:02
288 查看
Problem Description
CRB has two strings s and t.
In each step, CRB can select arbitrary character c of s and
insert any character d (d ≠ c)
just after it.
CRB wants to convert s to t.
But is it possible?
[b]Input
[/b]
There
are multiple test cases. The first line of input contains an integer T,
indicating the number of test cases. For each test case there are two strings s and t,
one per line.
1 ≤ T ≤ 105
1 ≤ |s| ≤ |t| ≤ 105
All strings consist only of lowercase English letters.
The size of each input file will be less than 5MB.
[b]Output
[/b]
For
each test case, output "Yes" if CRB can convert s to t, otherwise output "No".
题意:输入两串字符串s和t,在s中选择一个字母在其后边插入一个与其不同的字母,可以插入无限次,问通过插入操作是否能使s变为t。
思路:如果直接暴力遍历铁定会超时啊(233~。经过大神教导,该题是有规律的,只要该两个字符串符合两个条件就可以通过插入操作使s变为t。条件一:字符串s中字母出现的次数要小于等于字符串t中该字母出现的次数(毕竟是往字符串s中插入字母,如果s中的某字母比t中的多那么字符串s通过插入操作怎么也不可能变成t);条件二:字符串s的首字母与字符串t的首字母要相等,且如果两个字符串的开头是连续的相同字母,则s开头连续的相同字母的个数要大于等于t开头连续的相同字母个数(如果连续的相同字母出现在字符串中间,则可以通过在该字母之前插入其他字母将该连续的相同字母分开)。只要满足以上两个条件则可达到题目要求输出“Yes”,否则输出“No”.
[b]Sample
Input
[/b]
4
a
b
cat
cats
do
do
apple
aapple
[b]Sample
Output
[/b]
No
Yes
Yes
No
CRB has two strings s and t.
In each step, CRB can select arbitrary character c of s and
insert any character d (d ≠ c)
just after it.
CRB wants to convert s to t.
But is it possible?
[b]Input
[/b]
There
are multiple test cases. The first line of input contains an integer T,
indicating the number of test cases. For each test case there are two strings s and t,
one per line.
1 ≤ T ≤ 105
1 ≤ |s| ≤ |t| ≤ 105
All strings consist only of lowercase English letters.
The size of each input file will be less than 5MB.
[b]Output
[/b]
For
each test case, output "Yes" if CRB can convert s to t, otherwise output "No".
题意:输入两串字符串s和t,在s中选择一个字母在其后边插入一个与其不同的字母,可以插入无限次,问通过插入操作是否能使s变为t。
思路:如果直接暴力遍历铁定会超时啊(233~。经过大神教导,该题是有规律的,只要该两个字符串符合两个条件就可以通过插入操作使s变为t。条件一:字符串s中字母出现的次数要小于等于字符串t中该字母出现的次数(毕竟是往字符串s中插入字母,如果s中的某字母比t中的多那么字符串s通过插入操作怎么也不可能变成t);条件二:字符串s的首字母与字符串t的首字母要相等,且如果两个字符串的开头是连续的相同字母,则s开头连续的相同字母的个数要大于等于t开头连续的相同字母个数(如果连续的相同字母出现在字符串中间,则可以通过在该字母之前插入其他字母将该连续的相同字母分开)。只要满足以上两个条件则可达到题目要求输出“Yes”,否则输出“No”.
[b]Sample
Input
[/b]
4
a
b
cat
cats
do
do
apple
aapple
[b]Sample
Output
[/b]
No
Yes
Yes
No
<span style="font-size:18px;">#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <queue> #define INF 0x3f3f3f3f #define esp 1e-9 using namespace std; int s1[200],t1[200]; char s[110000],t[110000]; int main() { //freopen("lalala.text","r",stdin); int T; scanf("%d",&T); while(T--) { scanf("%s",s); scanf("%s",t); if(s[0]!=t[0]) //如果两个字符串的首字母都不同则肯定不会达到题目要求 { printf("No\n"); continue; } memset(s1,0,sizeof(s1)); memset(t1,0,sizeof(t1)); int slen=strlen(s); int tlen=strlen(t); int ss=0,tt=0; char ch=s[0]; int flag=0; for(int i=0; i<slen; i++) { if(s[i]==ch&&!flag) ss++; //累加字符串开头连续相同字母的个数 else flag=1; s1[s[i]]++; } ch=t[0]; flag=0; for(int i=0; i<tlen; i++) { if(t[i]==ch&&!flag) tt++; else flag=1; t1[t[i]]++; } flag=1; if(ss<tt) flag=0; else { for(int i=95; i<125; i++) { if(s1[i]>t1[i]) { flag=0; break; } } } if(flag) printf("Yes\n"); else printf("No\n"); } return 0; }</span><span style="font-weight: bold; font-size: 14px;"> </span>
相关文章推荐
- mapreduce查询mysql表中记录数
- Perfection
- 2015年8月20日21:03:18 firstNotRepeatingChar 哈希表实现O(n)
- 手工释放linux内存――/proc/sys/vm/drop_cache
- Hibernate 所有缓存机制详解
- 第90讲:基于Scala的Actor之上的分布式并发消息驱动框架Akka初体验学习笔记
- android 添加视频、图片、录音上传(二)
- [HDOJ4911]Inversion
- 电脑反应慢的原因
- NYOJ 709 异 形 卵(区域最大值,水题)
- [Linux]工作中常见的命令
- Win10下安装Genymotion出现virtualization engine not found的解决方案
- HDOJ 1241 Oil Deposits (DFS)
- C++那些细节--函数的默认参数
- poj 2406 Power Strings(kmp求一个串的重复子串)
- C语言中do...while(0)的妙用-避免goto
- OJ刷题---奇怪的分式
- hdu2066一个人的旅行【最短路dijkstra&&SPFA】
- Python|绝不乱入的靠谱书单
- golang-发送邮件