codeforces 495C. Treasure 解题报告

题目链接:http://codeforces.com/problemset/problem/495/C

题目意思:给出一串只有三种字符( ‘)‘,‘(‘ 和 ‘#‘)组成的字符串,每个位置的这个字符 ‘#‘可以替换成不少于 1 个的 ‘)‘,问如何对每个‘#‘进行替换,使得对于字符串的任意一个位置, ‘)‘ 的数量始终不大于‘(‘ 的数量。注意,‘#‘被替换成‘)‘的总数以及原先有的‘)‘的数量之和 == ‘(‘ 的总数。

  花了两个晚上的一点时间,今天在图书馆里终于想到解决方案了,大感动 ~~~~ >__<

/********************************************* (错误思路请忽略= =)

  第一个晚上做的时候,只知道最好的填法就是,除了最后一个‘#‘之外,其他都替换为一个 ‘)‘,最后一个就根据公式: 数量 ‘(‘ -  数量 ‘)‘ - (总的‘#‘数 - 1)  来填入。

  但是问题是,如何确定有解。我当时很天真地以为,如果 ‘(‘ == ‘)‘ 但是‘#‘还有剩(‘#‘ > 0)就无解,test7:#)))) 证明此想法是错的,本来答案是-1,我程序输出 -4,因为这个判断是有缺陷的,排除不了这种情况。后来干脆直接保存每个位置的‘(‘ (cnt[]) 和  ‘)‘(cnt1[])的数量还有 ‘#‘的数量,如果是‘(‘,cnt[i] = cnt[i-1]+1,cnt1[i] = cnt[i-1];如果是‘)‘,cnt1[i] = cnt1[i-1]+1,cnt[i] = cnt[i-1];如果是‘#‘,cnt[i] = cnt[i-1],cnt1[i] = cnt1[i-1]。‘(‘, ‘)‘, ‘#‘总数量分别对应c1, c2, c0,然后加多一个check()函数来判断如果cnt[i] < cnt1[i] 就 返回false,我只能说:有点不知所谓 = =....因为压根就没有考虑‘#‘ 的值!不知道‘#‘具体填什么【test12 ##((((((() 】。改改改接着就是test14........噩梦啊~~~~煎熬啊

  **********************************************/

  早上重新想过,终于有头绪了。我的做法应该叫做所谓的反证法吧~~~~我们知道最保守最可行的方法就是:除了最后一个‘#‘之外的所有 ‘#‘ 都替换为 一个‘)‘。

  设两个整型数tot 和c(初始值都为 0, c 用来保存‘#‘的数量),一个数组vis[],保存每个是‘#‘的位置的替换个数,遍历一遍数组s[i]

  if 【‘(‘】 tot++

  if 【‘)‘】 tot--

  if 【‘#‘】 c++, vis[i] = 1

  然后反向遍历数组s[i],找出最后一个‘#‘的位置,使得vis[i] = tot - (c-1)。但是前期要做一个判断工作,tot-(c-1) <= 0是无解的!,它专门处理 ‘(‘ == ‘)‘,c >= 1 的情况(类似(#),或者 #)。

  最后当然就是判断无解情况啦。开一个整数 cnt = 0,从前往后遍历数组s[],

  if 【‘(‘】 cnt++

  if 【‘)‘】 cnt--

  if 【‘#‘】 cnt -= vis[i]

  需要检查每个位置的cnt的值,if (cnt < 0) 就返回false。即表示 ) > ( ,不满足题目要求。

  最后返回true的情况是 cnt == 0,这样才能保证    ‘#‘替换为 ‘)‘ 以及本来存在的 ‘)‘的数量之和 等于‘(‘的数量

  如果经得起 check() 函数的考验,那么就输入解。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 using namespace std;
 6
 7 const int maxn = 1e5 + 5;
 8 char s[maxn];
 9 int vis[maxn], len;
10
11 bool check(char s[])
12 {
13     int cnt = 0;
14     for (int i = 0; i < len; i++)
15     {
16         if (s[i] == ‘(‘)
17             cnt++;
18         else if (s[i] == ‘)‘)
19             cnt--;
20         else             // #
21             cnt -= vis[i];
22         if (cnt < 0)      // 任意位置都要保证 ‘)‘ <= ‘(‘
23             return false;
24     }
25     if (cnt == 0)
26         return true;
27     return false;
28 }
29
30 int main()
31 {
32     #ifndef ONLINE_JUDGE
33         freopen("in.txt", "r", stdin);
34     #endif // ONLINE_JUDGE
35
36     while (scanf("%s", s) != EOF)
37     {
38         memset(vis, 0, sizeof(vis));
39
40         len = strlen(s);
41         int tot = 0, c = 0;
42         for (int i = 0; i < len; i++)
43         {
44             if (s[i] == ‘(‘)
45                 tot++;
46             else if (s[i] == ‘)‘)
47                 tot--;
48             else       // #
49             {
50                 c++;
51                 vis[i] = 1;
52             }
53         }
54         if (tot-(c-1) <= 0)
55             printf("-1\n");
56         else
57         {
58             int last = tot - (c-1);   // 最后一个 # 的赋值,至少要为1
59             for (int i = len-1; i >= 0; i--)
60             {
61                 if (s[i] == ‘#‘)
62                 {
63                     vis[i] = last;
64                     break;
65                 }
66             }
67             if (!check(s))
68                 printf("-1\n");
69             else
70             {
71                 for (int i = 1; i <= c-1; i++)
72                     printf("1\n");
73                 printf("%d\n", last);
74             }
75         }
76     }
77     return 0;
78 }
时间: 2024-08-10 19:15:43

codeforces 495C. Treasure 解题报告的相关文章

codeforces 499B.Lecture 解题报告

题目链接:http://codeforces.com/problemset/problem/499/B 题目意思:给出两种语言下 m 个单词表(word1, word2)的一一对应,以及 professor's lecture 的 n 个单词.问记下来的笔记是什么.对于professor's lecture 的某个单词,如果在单词表中找到,word1, word2 都有可能.如果 word1 的长度  <= word2 的长度,就输出word1,否则word2 考了map<string, st

codeforces 490B.Queue 解题报告

题目链接:http://codeforces.com/problemset/problem/490/B 题目意思:给出每个人 i 站在他前面的人的编号 ai 和后面的人的编号 bi.注意,排在第一个位置的人他前面是无人的!于是 a1 = 0.最后那个人的后面是木有人的,即 bn = 0.然后根据这些条件求出整个序列是如何排的,输出答案. 这条题卡了好久.........啊........啊........啊 首先很容易知道第二个位置的人的编号 和 倒数第二个人的位置编号.用一个aft[]数组记录

Codeforces #263 div2 解题报告

比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注册的时候很想好好的做一下,但是网上喝了个小酒之后,也就迷迷糊糊地看了题目,做了几题,一觉醒来发现rating掉了很多,那个心痛啊! 不过,后来认真的读了题目,发现这次的div2并不是很难! 官方题解:http://codeforces.com/blog/entry/13568 A. Appleman and Easy Task 解析: 一个水题,判断每个细胞周围是否都是有偶数个相邻细胞.   代码

Codeforces 474(#270) 解题报告

A:直接贴过来装换 解题代码 1 // File Name: a.cpp 2 // Author: darkdream 3 // Created Time: 2014年10月06日 星期一 23时28分57秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #include<deque> 10 #include<stack> 11 #inc

Codeforces Round #513解题报告(A~E)By cellur925

我是比赛地址 A:Phone Numbers $Description$:给你一串数字,问你能组成多少开头为8的11位电话号码. $Sol$:统计8的数量,与$n$%11作比较. 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 5 using namespace std; 6 7 int n,len,cnt,ans; 8 char ch[1000]; 9 10 int main() 11 {

Codeforces Round #301 解题报告

感觉这次的题目顺序很不合理啊... A. Combination Lock Scrooge McDuck keeps his most treasured savings in a home safe with a combination lock. Each time he wants to put there the treasures that he's earned fair and square, he has to open the lock. The combination loc

Codeforces Round #302 解题报告

感觉今天早上虽然没有睡醒但是效率还是挺高的... Pas和C++换着写... 544A. Set of Strings You are given a string q. A sequence of k strings s1, s2, ..., sk is called beautiful, if the concatenation of these strings is string q(formally, s1 + s2 + ... + sk = q) and the first chara

解题报告 之 CodeForces 91B Queue

解题报告 之 CodeForces 91B Queue Description There are n walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at the end of the queue and the n-th walrus stands at the beginning of the queue.

codeforces 505A. Mr. Kitayuta&#39;s Gift 解题报告

题目链接:http://codeforces.com/problemset/problem/505/A 题目意思:给出一个长度不大于10的小写英文字符串 s,问是否能通过在字符串的某个位置插入一个字母,使得新得到的字符串成为回文串. /**************************************(又到自我反省时刻) 做的时候,通过添加一个单位使得长度增加1,找出中点,检验前一半的位置,找出对称位置替换成对应的前一半位置的字符,然后原字符串剩下的部分追加到后面,再判断回文.但是由于