Codeforces - 1194C - From S To T - 子序列 - 排序
https://codeforces.com/contest/1194/problem/C
好像没什么好说的,要能构造s必须是t的子序列,并且相差的字符集合d是p的子集。
用双指针法求两遍子序列就可以了,甚至不需要sort,假如用桶排的话就是O(qn)的。
下面这个错在哪里呢?
正确的:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
char s[105];
char t[105];
char p[105];
char d[105];
bool is_sub1(char *s, char *t) {
int i = 0, j = 0, dl = 0;
int sl = strlen(s);
int tl = strlen(t);
while(i < sl && j < tl) {
if(s[i] == t[j]) {
i++;
j++;
} else {
d[dl++] = t[j];
j++;
}
}
if(i == sl) {
//s完全是t的子序列
while(j < tl) {
//把剩下的t都当做失配复制了
d[dl++] = t[j];
j++;
}
d[dl] = '\0';
sort(d, d + dl);
sort(p, p + strlen(p));
return true;
} else {
return false;
}
}
bool is_sub2(char *s, char *t) {
int i = 0, j = 0;
int sl = strlen(s);
int tl = strlen(t);
while(i < sl && j < tl) {
if(s[i] == t[j]) {
i++;
j++;
} else {
j++;
}
}
if(i == sl) {
return true;
} else {
return false;
}
}
int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
//freopen("Yinku.out", "w", stdout);
#endif // Yinku
while(~scanf("%d", &n)) {
while(n--) {
scanf("%s%s%s", s, t, p);
if(is_sub1(s, t) && is_sub2(d, p)) {
puts("YES");
} else {
puts("NO");
}
}
}
}
WA2的:
没有保证所有的i一定匹配,要是全部的j已经匹配完了其实也是失配了。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
char s[105];
char t[105];
char p[105];
char d[105];
bool is_sub1(char *s, char *t) {
int i = 0, j = 0, dt = 0;
int sl = strlen(s);
int st = strlen(t);
for(; i < sl; i++) {
while(j < st) {
if(t[j] != s[i]) {
d[dt++] = t[j];
j++;
if(j == st) {
return false;
}
} else {
j++;
break;
}
}
}
if(i == sl) {
while(j < st) {
d[dt++] = t[j];
j++;
}
sort(d, d + dt);
sort(p, p + strlen(p));
d[dt] = '\0';
//cout << d << endl;
//cout << p << endl;
return true;
} else {
return false;
}
}
bool is_sub2(char *s, char *t) {
int i = 0, j = 0;
int sl = strlen(s);
int st = strlen(t);
for(; i < sl; i++) {
while(j < st) {
if(t[j] != s[i]) {
j++;
if(j == st) {
return false;
}
} else {
j++;
break;
}
}
}
if(i == sl) {
return true;
} else {
return false;
}
}
int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
//freopen("Yinku.out", "w", stdout);
#endif // Yinku
while(~scanf("%d", &n)) {
while(n--) {
scanf("%s%s%s", s, t, p);
if(is_sub1(s, t) && is_sub2(d, p)) {
puts("YES");
} else {
puts("NO");
}
}
}
}
Codeforces - 1194C - From S To T - 子序列 - 排序的更多相关文章
- codeforces mysterious present 最长上升子序列+倒序打印路径
link:http://codeforces.com/problemset/problem/4/D #include <iostream> #include <cstdio> ...
- Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 离散化拓扑排序
C. Mail Stamps Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem ...
- Educational Codeforces Round 1 C. Nearest vectors 极角排序
Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/problem/ ...
- codeforces Gym 100500C D.Hall of Fame 排序
Hall of Fame Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/attachmen ...
- (CodeForces 510C) Fox And Names 拓扑排序
题目链接:http://codeforces.com/problemset/problem/510/C Fox Ciel is going to publish a paper on FOCS (Fo ...
- CodeForces - 598C Nearest vectors(高精度几何 排序然后枚举)
传送门: http://codeforces.com/problemset/problem/598/C Nearest vectors time limit per test 2 seconds me ...
- Codeforces 558E A Simple Task(计数排序+线段树优化)
http://codeforces.com/problemset/problem/558/E Examples input 1 abacdabcda output 1 cbcaaaabdd input ...
- Codeforces 526F Pudding Monsters - CDQ分治 - 桶排序
In this problem you will meet the simplified model of game Pudding Monsters. An important process in ...
- CodeForces - 154C:Double Profiles (hash+排序)
You have been offered a job in a company developing a large social network. Your first task is conne ...
随机推荐
- R语言抽样的问题
基本抽样函数sample sample(x,size,replace=F/T) x是数据集, size规定了从对象中抽出多少个数 replace 为F时候,表示每次抽取后的数就不能在下一次被抽取:T ...
- Spring Data Elasticsearch 用户指南
https://www.jianshu.com/p/27e1d583aafb 翻译自官方文档英文版,有删减. BioMed Central Development Team version 2.1.3 ...
- Linux系统Docker启动问题Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service"
在Liunx中使用Docker, 注: Liunx使用的是在虚拟机下的centOS7版本在刚开始安装Docker时没有任何错误, 但是在后续的docker启动过程中, 出现以下问题: [root@zk ...
- django之配置文件setting.py
一:配置文件setting.py中的简单配置更改 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 简单解释 ...
- php中换行怎么写
PHP中可以用PHP_EOL来替代,以提高代码的源代码级可移植性 unix系列用(推荐学习:PHP编程从入门到精通) 1 windows系列用 1 \r\n mac用 1 如: 1 2 3 4 5 6 ...
- 4412 移植x264并且YUV422转x264
转自http://blog.sina.com.cn/s/blog_af9acfc60101alxx.html 一.YUV422转换规律 做视频采集与处理,自然少不了要学会分析YUV数据.因为从采集的角 ...
- [luogu]P1016 旅行家的预算[贪心]
[luogu]P1016 旅行家的预算 题目描述 一个旅行家想驾驶汽车以最少的费用从一个城市到另一个城市(假设出发时油箱是空的).给定两个城市之间的距离D1.汽车油箱的容量C(以升为单位).每升汽油能 ...
- 20180826(04)-Java序列化
Java序列化 Java 提供了一种对象序列化的机制,该机制中,一个对象可以被表示为一个字节序列,该字节序列包括该对象的数据.有关对象的类型的信息和存储在对象中数据的类型 将序列化对象写入文件之后,可 ...
- Period POJ - 1961
Period POJ - 1961 时限: 3000MS 内存: 30000KB 64位IO格式: %I64d & %I64u 提交 状态 已开启划词翻译 问题描述 For each ...
- CocoaPods进阶:本地包管理
http://www.iwangke.me/2013/04/18/advanced-cocoapods/ 粉笔网的iOS工程师唐巧曾经写过一篇blog<使用CocoaPods来做iOS程序的包依 ...