UVA 129 - Krypton Factor
来源:互联网 发布:手机选购 知乎 编辑:程序博客网 时间:2024/05/16 13:50
#include <bits/stdc++.h>#define _ ios_base::sync_with_stdio(0);cin.tie(0);#define INF 0x3f3f3f3f#define eps 1e-6typedef long long LL;const double pi = acos(-1.0);const long long mod = 1e9 + 2015;using namespace std;int N,T;char s[105];int cnt;bool judge(int cur){ for(int i = 1;i <= (cur + 1) / 2;i++) { bool e = 1; for(int j = 0;j < i;j++) if(s[cur - i - j] != s[cur - j]) { e = 0; break; } if(e) return false; } return true;}void output(int cur){ for(int i = 0;i <= cur;i++) { if(i % 4 == 0 && i > 0) { if(i % 64 == 0 && i > 0) putchar('\n'); else putchar(' '); } putchar(s[i]); } printf("\n%d\n", cur + 1);}int dfs(int cur){ for(int i = 0;i < T;i++) { s[cur] = 'A' + i; if(judge(cur)) { cnt++; if(cnt == N) { s[cur + 1] = 0; output(cur); return 1; } if(dfs(cur + 1)) return 1; } } return 0;}int main(){ ios_base::sync_with_stdio(false); cin.tie(0); //freopen("int.txt","r",stdin); //freopen("out.txt","w",stdout); int first = 0; int t = 0; while(cin >> N >> T) { if(N == 0 && T == 0) break; cnt = 0; dfs(0); } return 0;}
#include <iostream>#include <string>using namespace std;//递归过程,顺序生成所有的字串。str保存结果,n和L为题目中给出的同名变量void Sequence(string &str, int &n, int L) { //记录字串长度,以加快运算 int nLen = str.length(), nHalf = (str.length() + 1) / 2; //在结果字串后面依次尝试添加前L个大写字母 for (char i = 'A', iEnd = L + 'A', m = 1; i < iEnd; ++i) { str.push_back(i); //插入当前字符 //下面判断新生成的字符串中是否存在相邻的重复 //第1次判断最后1个字符,第2次判断最后2个字符,以此类推 for (m = 1; m <= nHalf; ++m) { //将最后的i个字符与之前的i个字符比较,如果有相同则跳出 if (equal(str.end() - m, str.end(), str.end() - m * 2)) { m = 0; //将m置为0表示存在重复的相邻子串 break; //跳出循环 } } if (m != 0) { //如果不存在重复 //如果生成的字串已经够数,返回上一级 if (--n == 0) return; Sequence(str, n, L); //进入下一级调用 //如果生成的字串已经够数,返回上一级 if (n == 0) return; } //删除刚添加在后面的字符,保持结果字串在进入这一级时的原状 str.erase(nLen); //准备为添加下一个字符作准备 }}//主函数int main(void) { for (int n, L; cin >> n >> L && n != 0; ) { //循环读取每一组输入的数据 string str; //结果字符串 Sequence(str, n, L); //递归生成所有的无相邻重复字串 int nLen = str.length(); //保留生成字串的字符数量 for (size_t i = 4; i < str.length(); i += 5) { //按格式处理字串 //每隔4个插入一个空格,每隔80字符插入一个回车 str.insert(str.begin() + i, i == 79 ? '\n' : ' '); } //输出结果字串和字符数量 cout << str << '\n' << nLen << endl; } return 0;}
0 0
- uva 129 Krypton Factor
- uva 129 - Krypton Factor
- UVa 129 Krypton Factor
- UVA - 129 Krypton Factor
- UVa-129 - Krypton Factor
- UVa - 129 - Krypton Factor
- UVA - 129 Krypton Factor
- uva 129 - Krypton Factor
- uva 129 - Krypton Factor
- UVA 129 Krypton Factor
- UVA - 129 Krypton Factor
- UVa 129 Krypton Factor
- uva 129 Krypton Factor
- UVA 129 - Krypton Factor
- uva 129 Krypton Factor
- UVa-129-Krypton Factor
- UVA-129 Krypton Factor
- UVA 129 Krypton Factor
- AngularJS自定义指令directive:scope属性
- 【51nNod】-1007 正整数分组(01背包)
- Tensorflow一些常用基本概念与函数(3)
- 多线程安全性
- 解析XML的四种方式
- UVA 129 - Krypton Factor
- JavaScript引用类型——Function
- 通过okHttpUtils实现文件的上传下载
- Ucloud直播云SDK的简单集成
- Linux IO模式及 select、poll、epoll详解
- iOS开发 获取手机型号
- epoll用法详解 优点
- 【HDOJ 5834】Magic boy Bi Luo with his excited tree(树型DP)
- React Native自定义导航栏