紫书 例题7-5 UVA 129
来源:互联网 发布:yyf淘宝店地址 编辑:程序博客网 时间:2024/05/01 21:24
【题意】输入正整数n,l,输出由前l个字符组成的,字典序第k小的没有两个相邻重复子串的串。
【解题方法】判断ABCABA是否包含重复子串的方法是这样的:运用回溯法,只要比较当前串后缀,因为回溯法的特性,我们只要考虑当前的情况即可,前面的不用管,这样递归可以保证从头到尾都是成立的,当然后缀要从一个比较到n+1/2个(奇数多考虑一个),例如:ABCABA,先比较A与前面的B,再比较BA与CA,以此类推。
【AC代码】
////Created by just_sort 2016/1/5//Copyright (c) 2016 just_sort.All Rights Reserved////#include <ext/pb_ds/assoc_container.hpp>//#include <ext/pb_ds/tree_policy.hpp>//#include <ext/pb_ds/hash_policy.hpp>#include <set>#include <map>#include <queue>#include <stack>#include <cmath>#include <cstdio>#include <time.h>#include <cstdlib>#include <cstring>#include <sstream> //isstringstream#include <iostream>#include <algorithm>using namespace std;//using namespace __gnu_pbds;typedef long long LL;typedef pair<int, LL> pp;#define REP1(i, a, b) for(int i = a; i < b; i++)#define REP2(i, a, b) for(int i = a; i <= b; i++)#define REP3(i, a, b) for(int i = a; i >= b; i--)#define CLR(a, b) memset(a, b, sizeof(a))#define MP(x, y) make_pair(x,y)const int maxn = 1e3 + 10;const int maxm = 2e5;const int maxs = 10;const int maxp = 1e3 + 10;//const int INF = 1e9;const int UNF = -1e9;const int mod = 1e9 + 7;int gcd(int x, int y) {return y == 0 ? x : gcd(y, x % y);}//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>order_set;//headint n, L, cnt, qq;int s[100];int dfs(int cur){ if(cnt++ == n){ for(int i = 0; i < cur; i++){ qq++; printf("%c", 'A' + s[i]); if(qq % 64 == 0) printf("\n"); else if(qq % 4 == 0 && i != cur - 1) printf(" "); } printf("\n%d\n", qq); return 0; } for(int i = 0; i < L; i++){ s[cur] = i; int ok = 1; for(int j = 1; j * 2 <= cur + 1; j++){ int ok2 = 1; for(int k = 0; k < j; k++){ if(s[cur - k] != s[cur - k - j]){ ok2 = 0; break; } } if(ok2){ ok = 0; break; } } if(ok){ if(!dfs(cur + 1)) return 0; } } return 1;}int main(){ while(scanf("%d%d", &n, &L) != EOF) { if(n == 0 && L == 0) break; memset(s, 0, sizeof(s)); qq = cnt = 0; dfs(0); } return 0;}
0 0
- 紫书 例题7-5 UVA 129
- UVa #129 Krypton Factor (例题7-5)
- 紫书 例题7-7 UVA 1354
- 紫书 例题7-1 UVA 725
- 紫书 例题7-3 UVA 10976
- 紫书 例题7-4 UVA 524
- 紫书 例题7-6 UVA 140
- 紫书 例题7-8 Uva 10603 BFS
- 紫书 例题 7-11 宝箱 UVA 12325
- 紫书搜索 例题7-5 UVA
- UVA 11464 例题7
- 紫书动规 例题9-7 UVA
- uva 10474 例题5-1
- 紫书动规 例题9-5 UVA
- 例题7-5 UVA 129 Krypton Factor (困难的串,搜索)
- 例题7-5 困难的串(Krypton Factor, UVa 129)
- 紫书 例题7-2 UVA 11059 暴力枚举
- 紫书 例题 10-17 UVa 1639
- 修改ramdisk根文件系统方法
- [Medium]Maximal Square
- How tomcat works——3 连接器
- [Medium]Maximum Product Subarray
- [Easy]Best Time to Buy and Sell Stock
- 紫书 例题7-5 UVA 129
- [Medium]Triangle
- 集合之Collection、List
- HTTP状态码详解
- 夕拾算法初级篇:4)日期差值
- [Medium] Unique Binary Search Trees
- Node.js进程守护,防止程序崩溃自动退出
- [Medium]Decode Ways
- jsp页面不同跳转方式