Codeforces 559B Equivalent Strings 想法题
2015-08-14 12:31
357 查看
题目大意:
就是根据题目中给出的定义判断两个串是否相同
大致思路:
....前来补好久之前没补的题....
当时这个题比赛的时候直接递归根据定义用Hash判断的然后TLE了....
好吧毕竟不是正解
这个题的正确做法是每次将串分成两半按照字典序排序, 这样递归回溯排好之后, 如果两个串根据题目中的定义是相同的话, 那么这样做之后一定会使得两个串相同
并且时间复杂度也是严格意义上的O(nlogn)
代码如下:
Result : Accepted Memory : 400 KB Time : 31 ms
就是根据题目中给出的定义判断两个串是否相同
大致思路:
....前来补好久之前没补的题....
当时这个题比赛的时候直接递归根据定义用Hash判断的然后TLE了....
好吧毕竟不是正解
这个题的正确做法是每次将串分成两半按照字典序排序, 这样递归回溯排好之后, 如果两个串根据题目中的定义是相同的话, 那么这样做之后一定会使得两个串相同
并且时间复杂度也是严格意义上的O(nlogn)
代码如下:
Result : Accepted Memory : 400 KB Time : 31 ms
/* * Author: Gatevin * Created Time: 2015/8/14 12:20:00 * File Name: Sakura_Chiyo.cpp */ #include<iostream> #include<sstream> #include<fstream> #include<vector> #include<list> #include<deque> #include<queue> #include<stack> #include<map> #include<set> #include<bitset> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<cctype> #include<cmath> #include<ctime> #include<iomanip> using namespace std; const double eps(1e-8); typedef long long lint; #define maxn 200010 char s1[maxn], s2[maxn]; void change(char *s, int L, int R) { int len = (R - L + 1); if(len & 1) return; change(s, L, L + len/2 - 1); change(s, R - len/2 + 1, R); bool less = 1; for(int i = 0; i < len/2; i++) { if(s[L + i] < s[L + len/2 + i]) break; if(s[L + i] > s[L + len/2 + i]) { less = 0; break; } } if(!less) for(int i = 0; i < len/2; i++) swap(s[L + i], s[L + len/2 + i]); return; } int main() { scanf("%s", s1); scanf("%s", s2); int len = strlen(s1); change(s1, 0, len - 1); change(s2, 0, len - 1); for(int i = 0; i < len; i++) if(s1[i] != s2[i]) { puts("NO"); return 0; } puts("YES"); return 0; }
相关文章推荐
- BIGINT UNSIGNED value is out of range in..的解决方法
- FIneCMS /dayrui/libraries/Chart/ofc_upload_image.php Arbitrary File Upload Vul
- UVA 题目1521 GCD Guessing Game(数论+贪心)
- poj 2299 Ultra-QuickSort(树状数组)
- xcode工程文件格式(Xcode Project File Format)详解
- php require_once 的使用及与require的区别
- request对象的几个常用方法
- Win7安vc2008编译报LINK : fatal error LNK1000: Internal error during IncrBuildImage
- No enclosing instance of type WallpaperService is available due to some intermediate constructor inv
- 封装同步的UIActionSheet
- poj 3080 Blue Jeans
- Android.mk prebuild
- LeetCode:Unique Binary Search Trees Ⅱ
- hdu 1711 Number Sequence(kmp找子串第一次出现的位置)
- 使用4.3.11版本的hibernate结合druid连接池遇到的问题
- POJ 2533 Longest Ordered Subsequence
- easyui 密码框验证
- The Dole Queue
- UUID
- dlopen failed: empty/missing DT_HASH in "libx.so" (built with --hash-style=gnu?)