LCS Revised(大水题)

B - LCS Revised
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

Gym 100735G
Description
standard input/output
Statements
The longest common subsequence is a well known DP problem: given two strings A and B, one has to compute the maximum length of a subsequence that’s common to both A and B.

In this particular problem we work with strings A and B formed only by 0 and 1, having the same length. You’re given a string A of length n. Iterate all strings B possible. There are 2n of them. Calculate, for each string B, the longest common subsequence of A and B. Then, output the minimum length obtained.

Input
The first and the only line of the input contains string A, formed only by 0 and 1. It’s guaranteed that the length is between 1 and 105.

Output
Output a single number - the requested length.

Sample Input
Input
101010
Output
3

子序列不一定是连续的(我今天才知道,昨天比赛时还以为题目出错了,可是看到这题几乎都A了,我的内心是崩溃的。。。昨天我一直以为子序列必须是连续的)
感觉是个脑洞题,一个字符串中分别计算1和2的个数,个数小的的那个就是答案,我整不出来,但是代数可以发现这个规律。

#include <cstdio>
char s[100010];
int main()
{
    int a=0, b=0, i;
    scanf("%s", s);
    for(i=0;s[i];i++)
        if(s[i]=='0')
            ++a;
        else
            ++b;
    printf("%d", a<b?a:b);
}

你可能感兴趣的:(水题)