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 ...
随机推荐
- python常用函数 Y
yield有点像return,但他会在下一次执行的时候从上次结束点继续执行,带有 yield 的函数在 Python 中被称之为 generator(生成器),生成器无法通过索引获取数据,同时也承诺使 ...
- flask之jinjia2模板
一:渲染模板 app.run(debug=True) 开启debug模式,flask框架自动提示错误提示的页面显示. 视图函数 from flask import Flask from flask ...
- 小米笔记本pro版bios经常找不到硬盘
自从买了小米笔记本,对小米的印象大大折扣,bios经常找不到硬盘,关机,重启,就好了. 到小米售后,售后说是系统坏了,我说bios里都找不到.他说,系统坏了也会出现这个情况.我说好吧.重做后,没用几天 ...
- 解决kubernetes拉取不了镜像仓库的问题
将镜像仓库地址k8s.gcr.io改成registry.aliyuncs.com/google_containers
- C++ 得到系统时间
Time::Time() {//得到系统时间 初始化 time_t t; t=time(NULL); tm *lt; lt=localtime(&t); hour=lt->tm_hour ...
- C#如何获取系统downloads和documents路径
https://stackoverflow.com/questions/7672774/how-do-i-determine-the-windows-download-folder-path 如果你通 ...
- SQL element_at函数
库里有类似josn形式的字符串数据attr{"a":"123","b":"234"."c":&quo ...
- delphi 神经网络 学习
https://github.com/uldercarrilho/ParallelNeuralNetwork
- python实现基于两张图片生成圆角图标效果的方法
python实现基于两张图片生成圆角图标效果的方法 这篇文章主要介绍了python实现基于两张图片生成圆角图标效果的方法,实例分析了Python使用pil模块进行图片处理的技巧,分享给大家供大家参考. ...
- python 微服务方案
介绍 使用python做web开发面临的一个最大的问题就是性能,在解决C10K问题上显的有点吃力.有些异步框架Tornado.Twisted.Gevent 等就是为了解决性能问题.这些框架在性能上有些 ...