2014牡丹江——Known Notation

题目链接

  • 题意:

    输入一个长度不超过1000的字符串,包含数字(1-9)和星号(*)。字符串中的空格已经丢失,所以连起来的数字串能够看成很多分开的数。也能够看成连续的数,即能够随意加入空格。

    如今有两种操作:1)在任何位置加入随意类型的字符(数字或者星号)    2)交换字符串中的随意两个字符

    求:最少操作多少次,使得得到的串是一个合法的逆波兰式

  • 分析:

    对于n个星号,n+1个数字的字符串,假设将星号都移动到串的末尾。那么一定是合法的

    对于操作1,假设须要插入数字,那么插入到字符串的最前边是最优的

    对于操作2,仅仅可能将星号和数字交换。而且将星号移到了字符串的后边

    那么,先对串进行操作1,使得数字个数不小于星号的个数加一;然后从左到右扫描字符串,假设当前星号前边的数字和星号不满足相应关系(同上)。须要将当前星号与最后一个数字交换;最后,假设字符串最后不是星号。答案加一

  • 注意:

    假设最后得到的串最后不是星号。那么必定答案加一

    假设输入没有星号。那么答案是0

const int maxn = 1100;

char s[maxn];
int n;
vector<int> v;
int main()
{
    int T;
    RI(T);
    while (T--)
    {
        v.clear();
        RS(s);
        n = strlen(s);
        int num = 0, sig = 0;
        REP(i, n)
        {
            if (s[i] == ‘*‘) sig++;
            else
            {
                v.push_back(i);
                num++;
            }
        }
        int ans = 0;
        int preadd = 0;
        if (!sig)
        {
            ans = 0;
        }
        else
        {
            if (num < sig + 1)
            {
                preadd = sig + 1 - num;
                ans += preadd;
            }

            int sig_num = 0;

            for (int i = 0; i < n; i++)
            {
                if (s[i] == ‘*‘)
                {
                    sig_num++;
                    if (preadd < sig_num + 1)
                    {
                        int sz = v.size();
                        int id = v[sz - 1];
                        v.pop_back();
                        swap(s[i], s[id]);

                        sig_num--;
                        preadd++;
                        ans++;
                    }
                }
                else preadd++;
            }

            if (s[n - 1] != ‘*‘)
                ans++;
        }
        printf("%d\n", ans);

    }
    return 0;
}
时间: 2024-10-13 02:20:45

2014牡丹江——Known Notation的相关文章

2014牡丹江——Hierarchical Notation

题目链接 字符串模拟 const int MAXN = 2000000; char ipt[MAXN], t[MAXN]; int f[MAXN], len, to[MAXN]; map<string, string> mp[MAXN]; string x, key, ans; string i2s(int n) { string ret = ""; if (n == 0) ret += '0'; else { while (n) { ret += n % 10 + '0'

ZOJ 3829 Known Notation (2014牡丹江H题)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5383 Known Notation Time Limit: 2 Seconds      Memory Limit: 65536 KB Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. I

ACM学习历程——ZOJ 3829 Known Notation (2014牡丹江区域赛K题)(策略,栈)

Description Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. It is also known as postfix notation since every operator in an expression follows all of its operands. Bob is a student in

2014牡丹江K Known Notation

Known Notation Time Limit: 2 Seconds      Memory Limit: 65536 KB Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. It is also known as postfix notation since every operator in an expres

ZOJ 3826 Hierarchical Notation(2014 牡丹江 H,字符串模拟)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5380 Hierarchical Notation Time Limit: 2 Seconds      Memory Limit: 131072 KB In Marjar University, students in College of Computer Science will learn EON (Edward Object Notation), which is a

2014 牡丹江赛区总结

随着上海赛区比赛的结束,2014赛季也告一段落了.是时候总结一下.. 从网络赛开始..就深感到自己实力的不足,除了牡丹江网络赛中出了一道搜索+剪枝之外,似乎我就没有做出什么贡献..总是冒充Java专业选手写写高精度..上网百度模板什么的..尝试开了几次大模拟或者复杂搜索也没能够现场做出来.总之就是感觉自己能力十分不足. 然后我,Wzy,1243France作为2014年AHU的首发去了牡丹江.牡丹江基本上是被看做最弱的赛区吧.所以教练放心大胆的让我们三个13级的去,虽然结果是很遗憾的.我们到牡丹

2014牡丹江网络赛解题报告

The 2014 ACM-ICPC Asia Mudanjiang Regional First Round 题目链接 A题解题报告 B题解题报告 C题解题报告 D题解题报告 E题解题报告 F题解题报告 G题(未完成) H题解题报告 I题解题报告 J题解题报告

2014牡丹江区域赛H(字典树)ZOJ3826

Hierarchical Notation Time Limit: 2 Seconds      Memory Limit: 131072 KB In Marjar University, students in College of Computer Science will learn EON (Edward Object Notation), which is a hierarchical data format that uses human-readable text to trans

2014牡丹江区域赛K(贪心)ZOJ3829

Known Notation Time Limit: 2 Seconds      Memory Limit: 131072 KB Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. It is also known as postfix notation since every operator in an expre