Gray code(简单dp水题)

Gray code

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1403    Accepted Submission(s): 749

 

Problem Description

The reflected binary code, also known as Gray code after Frank Gray, is a binary numeral system where two successive values differ in only onebit (binary digit). The reflected binary code was originally designed to prevent spurious output from electromechanical switches. Today, Gray codes are widely used to facilitate error correction in digital communications such as digital terrestrial television and some cable TV systems.



Now , you are given a binary number of length n including ‘0’ , ’1’ and ‘?’(? means that you can use either 0 or 1 to fill this position) and n integers(a1,a2,….,an) . A certain binary number corresponds to a gray code only. If the ith bit of this gray code is 1,you can get the point ai.
Can you tell me how many points you can get at most?

For instance, the binary number “00?0” may be “0000” or “0010”,and the corresponding gray code are “0000” or “0011”.You can choose “0000” getting nothing or “0011” getting the point a3 and a4.

Input

The first line of the input contains the number of test cases T.

Each test case begins with string with ‘0’,’1’ and ‘?’.

The next line contains n (1<=n<=200000) integers (n is the length of the string).

a1 a2 a3 … an (1<=ai<=1000)

Output

For each test case, output “Case #x: ans”, in which x is the case number counted from one,’ans’ is the points you can get at most

Sample Input

2

00?0

1 2 4 8

????

1 2 4 8

Sample Output

Case #1: 12

Case #2: 15

Hint

https://en.wikipedia.org/wiki/Gray_code http://baike.baidu.com/view/358724.htm

简单dp。就是算格雷码。当前一位和上一位不一样就是1,一样就是0.

状态判断下转移就好了。

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=200010;
int dp[maxn][3];
int money[maxn];
char s[maxn];
int main()
{
    int t;
    int cas=0;
    scanf("%d",&t);
    while(t--){
        cas++;
        memset(dp,0,sizeof(dp));
        scanf("%s",s);
        int len=strlen(s);
        for(int i=0;i<len;i++){
            scanf("%d",&money[i]);
        }
        int ans=0;
        if(s[0]=='0'){
            dp[0][0]=0;
            dp[0][1]=0;
        }
        else {
            dp[0][1]=money[0];
            dp[0][0]=0;
        }
        for(int i=1;i<len;i++){
            if(s[i]=='0'){
                if(s[i-1]=='0')
                    dp[i][0]=dp[i-1][0];
                else if(s[i-1]=='1'){
                    dp[i][0]=dp[i-1][1]+money[i];
                }
                else{
                    dp[i][0]=max(dp[i-1][1]+money[i],dp[i-1][0]);
                }
            }
            else if(s[i]=='1'){
                if(s[i-1]=='0'){
                    dp[i][1]=dp[i-1][0]+money[i];
                }
                else if(s[i-1]=='1'){
                    dp[i][1]=dp[i-1][1];
                }
                else{
                    dp[i][1]=max(dp[i-1][0]+money[i],dp[i-1][1]);
                }
            }
            else{
                if(s[i-1]=='0'){
                    dp[i][0]=dp[i-1][0];
                    dp[i][1]=dp[i-1][0]+money[i];
                }
                else if(s[i-1]=='1'){
                    dp[i][1]=dp[i-1][1];
                    dp[i][0]=dp[i-1][1]+money[i];
                }
                else{
                    dp[i][1]=max(dp[i-1][0]+money[i],dp[i-1][1]);
                    dp[i][0]=max(dp[i-1][0],dp[i-1][1]+money[i]);
                }
            }
        }
        if(s[len-1]=='0'){
            printf("Case #%d: %d\n",cas,dp[len-1][0]);
        }
        else if(s[len-1]=='1'){
            printf("Case #%d: %d\n",cas,dp[len-1][1]);
        }
        else{
            int haha=max(dp[len-1][1],dp[len-1][0]);
            printf("Case #%d: %d\n",cas,haha);
        }
        
    }
    return 0;
}

 

 

 

Author

UESTC

<think>好的,我现在需要解决用户在使用OpenCV的HoughCircles函数时遇到的断言失败问,错误信息是“Assertion failed image type CV_8UC1”。首先,我得理解这个错误的原因。根据用户提供的引用资料,类似的OpenCV错误通常是因为输入图像的数据类型或通道数不符合函数要求。比如引用4和5提到,直方图均衡化和阈值分割需要输入是CV_8UC1类型,即8位无符号单通道图像(灰度图)。 HoughCircles函数同样需要输入图像是单通道的灰度图像,也就是CV_8UC1类型。如果用户传入的是彩色图像(CV_8UC3),或者虽然是单通道但数据类型不是8位无符号(比如16位或浮点型),就会触发这个断言错误。 接下来,我需要考虑用户可能的错误场景。例如,用户可能没有将图像转换为灰度图就直接使用HoughCircles,或者转换过程中出现了问,比如数据类型未正确转换。此外,用户可能在图像预处理步骤中使用了某些操作,改变了图像的类型或通道数,比如中值滤波后未保持8位单通道,或者保存图像时格式不正确。 解决方案的第一步应该是检查输入图像的类型和通道数。用户可以使用img.shape和img.dtype来确认这些信息。如果图像是彩色,必须用cv2.cvtColor转换为灰度图。同时,需要确保转换后的图像确实是CV_8UC1类型,例如使用img.astype(np.uint8)来确保数据类型正确。 另外,用户可能在预处理步骤中进行了其他操作,比如滤波或二值化,导致图像类型改变。例如,引用5中提到,阈值分割前必须确保图像是灰度格式。因此,用户需要检查整个处理流程中的每一步,确保图像类型没有被意外修改。 最后,给出具体的代码示例会有帮助。用户需要先读取图像,转换为灰度,必要时调整数据类型,再进行HoughCircles检测。同时,提醒用户注意图像路径是否正确,避免其他错误。 总结,问根源在于输入图像不符合HoughCircles的要求,解决方法是确保输入为8位单通道灰度图像,并检查所有预处理步骤是否保持正确的类型。</think>### OpenCV中HoughCircles函数断言失败问解决方法 当使用OpenCV的`cv2.HoughCircles`函数时,若出现`Assertion failed image type CV_8UC1`错误,通常是由于输入图像的类型或通道数不符合要求。以下是逐步解决方案: --- #### **1. 错误原因分析** - **断言条件要求**:`HoughCircles`函数要求输入图像必须是单通道、8位无符号整型(`CV_8UC1`),即灰度图像[^4][^5]。 - **常见错误场景**: 1. 直接传入彩色图像(3通道,`CV_8UC3`) 2. 图像数据类型非`uint8`(如`float32`或`uint16`) 3. 预处理步骤中意外修改了图像类型 --- #### **2. 解决方法步骤** ##### **步骤1:检查输入图像类型** 使用以下代码验证图像类型: ```python import cv2 img = cv2.imread("input.jpg") print("图像形状(通道数):", img.shape) # 若输出(高,宽,3)则为彩色图 print("图像数据类型:", img.dtype) # 需输出uint8 ``` ##### **步骤2:转换为灰度图像** 若图像为彩色,需强制转换为单通道: ```python gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ``` ##### **步骤3:确保数据类型正确** 若图像已经是灰度图但类型不符,需显式转换: ```python gray = gray.astype(np.uint8) ``` ##### **步骤4:验证预处理流程** 若使用了滤波、二值化等操作,需确保输出仍为`CV_8UC1`: ```python # 示例:中值滤波后保持类型一致 blurred = cv2.medianBlur(gray, 5) ``` ##### **步骤5:调用HoughCircles** ```python circles = cv2.HoughCircles( blurred, cv2.HOUGH_GRADIENT, dp=1, minDist=20, param1=50, param2=30, minRadius=0, maxRadius=0 ) ``` --- #### **3. 完整代码示例** ```python import cv2 import numpy as np # 读取图像并验证 img = cv2.imread("input.jpg") if img is None: raise ValueError("图像路径错误") # 转换为灰度图并检查类型 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = gray.astype(np.uint8) # 预处理(可选) blurred = cv2.medianBlur(gray, 5) # 霍夫圆检测 circles = cv2.HoughCircles( blurred, cv2.HOUGH_GRADIENT, dp=1, minDist=20, param1=50, param2=30, minRadius=0, maxRadius=0 ) # 绘制结果 if circles is not None: circles = np.uint16(np.around(circles)) for (x, y, r) in circles[0, :]: cv2.circle(img, (x, y), r, (0, 255, 0), 2) cv2.imshow("Result", img) cv2.waitKey(0) ``` --- #### **4. 注意事项** - 若图像本身是16位深度(如医学影像),需先通过`cv2.normalize`转换为8位: ```python gray = cv2.normalize(gray, None, 0, 255, cv2.NORM_MINMAX, dtype=cv2.CV_8U) ``` - 避免使用`cv2.imread`时指定`cv2.IMREAD_UNCHANGED`,可能保留异常数据类型 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值