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 ...
随机推荐
- MiniUI学习笔记1-新手必读
1.mini的全局方法 2.Ajax jQuery 拥有完整的 Ajax 兼容套件.其中的函数和方法允许我们在不刷新浏览器的情况下从服务器加载数据. 详细jQuery Ajax教程,可参考这里. 3. ...
- SICP 习题解 第二章
计算机程序的构造和解释习题解答 Structure and Interpretation os Computer Programs Exercises Answer 第二章 构造数据抽象 练习2.17 ...
- html5 新增和改良的input 类型实例
url test1.html <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- nodejs和npm之间的关系
Node.js是JavaScript的一种运行环境,是对Google V8引擎进行的封装.是一个服务器端的javascript的解释器. 包含关系,nodejs中含有npm,比如说你安装好nodejs ...
- 034:DTL常用过滤器(3)
default过滤器: 如果值被评估为 False .比如 [] , "" , None , {} 等这些在 if 判断中为 False 的值,都会使用 default 过滤器提供 ...
- JDBC连接Hive数据库
一.依赖 pom <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncodi ...
- NOIP2011提高组 Day1 T3 Mayan游戏
题目描述 Mayan puzzle是最近流行起来的一个游戏.游戏界面是一个7行×5列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上.游戏通关是指在规定的步 ...
- php选择文件夹上传
最近遇见一个需要上传百兆大文件的需求,调研了七牛和腾讯云的切片分段上传功能,因此在此整理前端大文件上传相关功能的实现. 在某些业务中,大文件上传是一个比较重要的交互场景,如上传入库比较大的Excel表 ...
- POJ 3660 Cow Contest ( 最短路松弛思想应用 && Floyd求传递闭包 )
题意 : 给出 N 头奶牛在比赛的结果,问你最多的能根据给出结果确定其名次的奶牛头数.结果给出的形式为 A B 代表在比赛当中 A 战胜了 B 分析 : 对于一头奶牛来说,如果我们能确定其他 N - ...
- [14th CSMO Day 1 <平面几何>]
关于LowBee苦思冥想的结果(仅供参考):