reverse (模拟)
来源:互联网 发布:ubuntu安装中文输入法 编辑:程序博客网 时间:2024/06/05 19:02
reverse
11.2
所有的由A和B构成的字符串,在这种操作下形成了一棵二叉树。根是空串,从根开始,向左走表示施加1号操作,向右走施加2号操作。
知道一个串,我们可以根据它最后一个字符是A还是B把它的父亲还原回来,所以我们有了在这棵树上遍历的能力。
做法就很显然了,我们只需要先让长的那个串一直向上走,知道他们深度相同,然后一起向上走,直到走到同一点,每走一步我们需要O(长度)的时间,最多走O(长度)步,其中。所谓的字典序最小呀都是来骗你的,因为任意长度的满足条件的只有一个串^_^。
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>#include <queue>#define LL long long#define N 3010using namespace std;int lena, lenb, flag = 0;char a[N], b[N];inline void reva(int pos){ for(register int i=1; i<=(pos + 1 >> 1); i++){ swap(a[i], a[pos-i+1]); }}inline void revb(int pos){ for(register int i=1; i<=(pos + 1 >> 1); i++){ swap(b[i], b[pos-i+1]); }}int main(){ freopen ("reverse.in", "r", stdin); freopen ("reverse.out", "w", stdout); int T; scanf("%d", &T); while ( T-- ){ flag = 0; scanf("%s%s", a+1, b+1); lena = strlen( a + 1 ); lenb = strlen( b + 1 ); while (lena > lenb){ --lena; if(a[lena+1] == 'B') reva( lena ); } while (lena < lenb){ --lenb; if(b[lenb+1] == 'B') revb( lenb ); } while ( true ){ for(int i=1; i<=lena; i++) if(a[i] != b[i]) { flag = 1; break;} if( !flag ){ for(int i=1; i<=lena; i++) printf("%c", a[i]); printf("\n"); break;} if(lena == 1) { printf("-1\n"); break;} --lena; if(a[lena+1] == 'B') reva( lena ); --lenb; if(b[lenb+1] == 'B') revb( lenb ); flag = 0; } }}
阅读全文
0 0
- reverse (模拟)
- hdu1266 Reverse Number (模拟)
- 反转键索引模拟实验(reverse key index)
- hdu 1062 Text Reverse(字符串处理,简单模拟)
- CodeForces - 658A Bear and Reverse Radewoosh (模拟)水
- Codeforces--658A--Bear and Reverse Radewoosh(模拟)
- HDU 1266 Reverse Number(模拟or数字字符串)
- CodeForces 658A Bear and Reverse Radewoosh(模拟)
- HDU 1062 Text Reverse (模拟)
- js模拟反转数组reverse()
- PC 110502 Reverse and Add //水题 字符串 模拟
- ACM-ICPC 2014北京邀请赛 H Happy Reverse [模拟]
- hdu1062(Text Reverse)
- 字符串反序(Reverse)
- reverse bit (!!!)
- HDOJ1062(Text Reverse)
- Text Reverse(1062)
- Reverse String(easy)
- Wigner-Ville 分布
- canvas与svg的区别(面试题)
- Python-用函数实现9*9乘法口诀
- 网络编程基础(一)
- hadoop源码解读一
- reverse (模拟)
- zabbix监控api接口性能及可用性 天气预报api为例
- 友好界面的爬虫监控系统
- 1097 最小公倍数
- Core Audio Overview
- 管理学之人际关系理论
- 日志采集系统filebeat输出到logstash配置
- Luogu 2827/bzoj 4721(单调队列)
- 结构体及位段