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 - 子序列 - 排序的更多相关文章

  1. codeforces mysterious present 最长上升子序列+倒序打印路径

    link:http://codeforces.com/problemset/problem/4/D #include <iostream> #include <cstdio> ...

  2. 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 ...

  3. Educational Codeforces Round 1 C. Nearest vectors 极角排序

    Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/problem/ ...

  4. codeforces Gym 100500C D.Hall of Fame 排序

    Hall of Fame Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/attachmen ...

  5. (CodeForces 510C) Fox And Names 拓扑排序

    题目链接:http://codeforces.com/problemset/problem/510/C Fox Ciel is going to publish a paper on FOCS (Fo ...

  6. CodeForces - 598C Nearest vectors(高精度几何 排序然后枚举)

    传送门: http://codeforces.com/problemset/problem/598/C Nearest vectors time limit per test 2 seconds me ...

  7. Codeforces 558E A Simple Task(计数排序+线段树优化)

    http://codeforces.com/problemset/problem/558/E Examples input 1 abacdabcda output 1 cbcaaaabdd input ...

  8. Codeforces 526F Pudding Monsters - CDQ分治 - 桶排序

    In this problem you will meet the simplified model of game Pudding Monsters. An important process in ...

  9. CodeForces - 154C:Double Profiles (hash+排序)

    You have been offered a job in a company developing a large social network. Your first task is conne ...

随机推荐

  1. python中的垃圾回收机制及原理

    序言: 来一起看看: 不同于C/C++,像Python这样的语言是不需要程序员写代码来管理内存的,它的GC(Garbage Collection)机制 实现了自动内存管理.GC做的事情就是解放程序员的 ...

  2. 脚本_使用awk提取linux主机参数

    #!bin/bash#功能:使用awk提取Linux主机的参数信息,如内容剩余容量,根分区剩余容量,本机IP,本机能登录的用户个数,CPU负载.#作者:liusingbon#使用awk提取内存剩余容量 ...

  3. LightOJ 1289 LCM from 1 to n(位图标记+素数筛

    https://vjudge.net/contest/324284#problem/B 数学水题,其实就是想写下位图..和状压很像 题意:给n让求lcm(1,2,3,...,n),n<=1e8 ...

  4. Java垃圾回收【GC】机制详解

    一.为什么需要垃圾回收 如果不进行垃圾回收,内存迟早都会被消耗空,因为我们在不断的分配内存空间而不进行回收.除非内存无限大,我们可以任性的分配而不回收,但是事实并非如此.所以,垃圾回收是必须的. 二. ...

  5. IDEA中项目引入独立包打包失败问题解决(找不到包)

    在terminal中执行以下命令:mvn install:install-file -DgroupId=ocx.GetRandom -DartifactId=GetRandom -Dversion=1 ...

  6. BZOJ 1911 特别行动队 (斜率优化)

    $ BZOJ~1911~*~ $ 特别行动队: (斜率优化) $ solution: $ 感觉这道题目还是比较常规的,首先我们很容易想到DP,因为题目里面说了选出的人都是连续的,这意味着我们可以从前往 ...

  7. 37行代码构建无状态组件通信工具-让恼人的Vuex和Redux滚蛋吧!

    状态管理的现状 很多前端开发者认为,Vuex和Redux是用来解决组件间状态通信问题的,所以大部分人仅仅是用于达到状态共享的目的.但是通常Redux是用于解决工程性问题的,用于分离业务与视图,让结构更 ...

  8. man arch

    ARCH(1)   Linux Programmer?. Manual/Linux程序员手册       ARCH(1) NAME/名称       arch - print machine arch ...

  9. Delphi GridPanel Percent百分比设置

    可能很多人都有这个困扰,为什么每次设置一个百分比后,值都会改变,只有设置成absolute​才会正常,经摸索发现,是因为精度引起,设置percent的时候,需要将精确到多个小数位.如要有3列,需要设置 ...

  10. C#在WinForm开发中Label换行方法

    很多朋友都会在开发WinForm中遇到Label要显示的内容太长,但却不能换行的问题.这里我总结了几种方法,供大家参考. 第一种是把Label的AutoSize属性设为False,手动修改Label的 ...