uva 11127 - Triple-Free Binary Strings
来源:互联网 发布:车辆识别软件 手机 编辑:程序博客网 时间:2024/06/06 04:39
Problem J
Triple-Free Binary Strings
Input: Standard Input
Output: Standard Output
A binary string consists of ones and zeros. Given a binary string T, if there is no binary string S such that SSS (concatenate three copies of S together) is a substring of T, we say T is triple-free.
A pattern consists of ones, zeros and asterisks, where an asterisk(*) can be replaced by either one or zero. For example, the pattern 0**1 contains strings 0001, 0011, 0101, 0111, but not 1001 or 0000.
Given a pattern P, how many triple-free binary strings does it contain?
InputEach line of the input represents a test case, which contains the length of pattern, n(0<n<31), and the pattern P. There can be maximum 35 test cases.
The input terminates when n=0.
OutputFor each test case, print the case number and the answer, shown below.
Sample Input Output for Sample Input
4 0**1
5 *****
10 **01**01**
0
Case 1: 2
Case 2: 16
Case 3: 9
Problemsetter: Rujia Liu
Special Thanks: Shahriar Manzoor
枚举出所有的字符串,再判断肯定要超时。所以需要剪枝,就是末尾每多考虑一个字符,就判断因这个字符是否会引起不合法,不合法则剪枝。
#include <cstdio>#include <algorithm>#include <vector>#include <map>#include <queue>#include <iostream>#include <stack>#include <set>#include <cstring>#include <stdlib.h>#include <cmath>using namespace std;typedef long long LL;typedef pair<int, int> P;const int maxn = 100+5;const int INF = 1000000000;char s[maxn];int n;bool judge(int d){ for(int i = 1;i*3 <= (d+1);i++){ int tag = 1; for(int j = d;j >= d-i+1;j--){ if(s[j] != s[j-i] || s[j] != s[j-2*i]){ tag = 0; break; } } if(tag == 1) return false; } return true;}int dfs(int d){ if(d == n){ return 1; } int ret = 0; if(s[d] == '*'){ s[d] = '0'; if(judge(d)) ret += dfs(d+1); s[d] = '1'; if(judge(d)) ret += dfs(d+1); s[d] = '*'; } else{ if(judge(d)) ret += dfs(d+1); } return ret;}int main(){ int kase = 0; while(scanf("%d", &n)){ kase++; if(n == 0) break; scanf("%s", s); printf("Case %d: %d\n", kase, dfs(0)); } return 0;}
0 0
- uva 11127 - Triple-Free Binary Strings(回溯)
- uva 11127 - Triple-Free Binary Strings
- UVA 11127 - Triple-Free Binary Strings
- UVA - 11127 Triple-Free Binary Strings dfs
- UVa11127 - Triple-Free Binary Strings
- UVa11127 - Triple-Free Binary Strings
- UVA 11127 - Triple-Free Binary Strings(二进制+搜索+位运算)
- 11127 - Triple-Free Binary Strings(dfs+位运算)
- UVA11127- Triple-Free Binary Strings(DFS+位运算)
- triple
- Triple
- uva 11081 strings
- UVA 11081 Strings
- UVA 455 - Periodic Strings
- uva 11081 - Strings(LCS)
- UVA 11081 Strings
- UVA - 11081 Strings
- UVA 11081 Strings(dp)
- mysql命令行
- Android: “Class loader may fail for processes that host multiple applications”【转】
- SDUT2876_走楼梯(大数)
- neicun
- oracle默认监听端口更改
- uva 11127 - Triple-Free Binary Strings
- 大话数据结构——读书笔记(2)
- Service的自我认知
- ssh连接 反应慢的解决办法
- 97 调用其他程序中Activity
- Eclipse初次启动时出现Failed to create the Java Virtual Machine的解决方案
- 函数调用的入栈顺序
- 会话处理程序(Session Handlers)
- 【剑指offer】数字在排序数组中出现的次数