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 ...
随机推荐
- left join on and和left join on where条件的困惑[转]
外连接:left join(左联接) left outer join 返回包括左表中的所有记录和右表中联结字段相等的记录right join(右联接) right outer join返回包括右表中的 ...
- idea旗舰版续命
首先鼓励大家使用正版!我一直用的是免费的社区版,但发现有一些功能被阉割了,比如weblogbic就不支持,无奈现在的项目要用到这个,只能去下载旗舰版. 旗舰版需要付费,作为程序员的我为了一个临时的项目 ...
- linux 验证 NFS 是否成功
服务器端----->>客户端 1. 服务器端 [root@allentuns ~]# ifconfig |grep "Bcast" inet addr:192.168. ...
- 5-基于TMS320C6678+XC7K325T的6U CPCIe高性能处理平台
基于TMS320C6678+XC7K325T的6U CPCIe高性能处理平台 一.板卡概述 本板卡系自主研发,基于CPCI 6U架构,符合CPCI2.0标准.采用 DSP TMS320C66 ...
- Linux架构之Nginx 负载均衡会话保持
案例No.50:Nginx负载均衡会话保持 前期准备环境 web01.web02 (web01.web02.db01.nfs01都要优化基本源)[root@web01 ~]# vim /etc/yum ...
- poj 2187 Beauty Contest(平面最远点)
Beauty Contest Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 24431 Accepted: 7459 D ...
- k8s阅读笔记3-k8s的网络解析
前言 阅读地址https://rootsongjc.gitbooks.io/kubernetes-handbook/content/concepts/flannel.html k8s客户端的启动 顺序 ...
- CSS3 Animations
CSS Animations 是CSS的一个模块,它定义了如何用关键帧来随时间推移对CSS属性的值进行动画处理.关键帧动画的行为可以通过指定它们的持续时间,它们的重复次数以及它们如何重复来控制. an ...
- Django登录(含随机生成图片验证码)注册实例
登录,生成随机图片验证码 一.登录 - 随机生成图片验证码 1.随机生成验证码 Python随机生成图片验证码,需要使用PIL模块,安装方式如下: pip3 install pillow 1)创建图片 ...
- linux运维、架构之路-linux定时任务
1.基础优化之开机启动服务优化 使用awk拼接的方式 [root@cache01 ~]# chkconfig |egrep -v "crond|network|sshd|rsyslog|sy ...