首页 > ACM题库 > HDU-杭电 > HDU 3242-List Operations-模拟-[解题报告]HOJ
2014
03-09

HDU 3242-List Operations-模拟-[解题报告]HOJ

List Operations

问题描述 :

A list is a sequence of zero or more elements, expressed in this form: [a1, a2, a3, ... , an], where each ai is one or more consecutive digits or lowercase letters. i.e. each list begins with a left square bracket, then zero or more elements separated by a single comma, followed by a right square bracket. There will be no whitespace characters (spaces, TABs etc) within a list.

In this problem, we use two list operations: append (++) and remove (–).
1. A ++ B: append elements in B to the end of A.
2. A — B: remove all the elements in B, from A. If something appears more than once in B, remove it that many times in A. If there are many equal elements in A to choose from, remove them from left to right (until all occurrences are removed, or there is no need to remove more elements).

Your task is to write a calculator, evaluating simple expressions or the form “list1 ++ list2” or “list1 — list2”.

输入:

There will be at most 10 expressions, one in each line, each having the form “list1 ++ list2” or “list1 — list2”, with no more than 80 characters in total (not counting the newline character). There will be exactly two spaces in each line: one before and one after the operator. Input ends with a single dot. The input is guaranteed to satisfy the restrictions stated above

输出:

There will be at most 10 expressions, one in each line, each having the form “list1 ++ list2” or “list1 — list2”, with no more than 80 characters in total (not counting the newline character). There will be exactly two spaces in each line: one before and one after the operator. Input ends with a single dot. The input is guaranteed to satisfy the restrictions stated above

样例输入:

[1,2,3] ++ [1,2,3]
[a,b,c,t,d,e,t,x,y,t] -- [t]
[a,b,c,t,d,e,t,x,y,t] -- [t,t,t,t]
[123] ++ [456]
.

样例输出:

[1,2,3,1,2,3]
[a,b,c,d,e,t,x,y,t]
[a,b,c,d,e,x,y]
[123,456]
4

->题目请戳这里<-

题目大意:给2个集合,每个集合用一对[ ]括起来,里面有数字和小写字母组成的元素,用","隔开,现在给2个集合以及2种操作:++操作,把第二个集合的元素合并到第一个集合中;–操作,就是从第一个集合中去掉2个集合重复的元素,去完为止。注意这里的集合中可以有重复的元素。

题目分析:简单模拟水之。

详情请见代码:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int N = 10005;
char str[N];
struct node
{
    char s[81];
    int len;
    int flag;
}list1[N],list2[N];
int n1,n2;
int op;

void solve()
{
    int len = strlen(str);
    memset(list1,0,sizeof(list1));
    memset(list2,0,sizeof(list2));
    n1 = n2 = 0;
    int i,j;
    for(i = 1;i < len;)
    {
        while(str[i] != ',' && str[i] != ']')
            list1[n1].s[list1[n1].len ++] = str[i ++];
        list1[n1].s[list1[n1].len] = '\0';
        n1 ++;
        if(str[i] == ']')
            break;
        i ++;
    }
    i +=2;
    if(str[i] == '+')
        op = 1;
    else
        op = 0;
    i += 4;
    for(;i < len - 1;)
    {
        while(str[i] != ',' && str[i] != ']')
            list2[n2].s[list2[n2].len ++] = str[i ++];
        list2[n2].s[list2[n2].len] = '\0';
        n2 ++;
        i ++;
    }
    if(op == 1)
    {
        printf("[%s",list1[0].s);
        for(i = 1;i < n1;i ++)
            printf(",%s",list1[i].s);
        for(i = 0;i < n2;i ++)
            printf(",%s",list2[i].s);
        printf("]\n");
        return;
    }
    for(i = 0;i < n2;i ++)
    {
        for(j = 0;j < n1;j ++)
        {
            if(list1[j].flag)
                continue;
            if(strcmp(list1[j].s,list2[i].s) == 0)
            {
                list1[j].flag = 1;
                break;
            }
        }
    }
    printf("[");
    for(j = n1 - 1;j >= 0;j --)
        if(list1[j].flag == 0)
            break;
    for(i = 0;i < j;i ++)
    {
        if(list1[i].flag == 0)
            printf("%s,",list1[i].s);
    }
    printf("%s]\n",list1[j].s);
}

int main()
{
    while(gets(str))
    {
        if(str[0] == '.')
            break;
        solve();
    }
    return 0;
}
/*
[1,2,3,2,3] -- [2,3,2]
*/

参考:http://blog.csdn.net/ophunter_lcm/article/details/9072049


  1. 站长,你好!
    你创办的的网站非常好,为我们学习算法练习编程提供了一个很好的平台,我想给你提个小建议,就是要能把每道题目的难度标出来就好了,这样我们学习起来会有一个循序渐进的过程!

  2. 漂亮。佩服。
    P.S. unsigned 应该去掉。换行符是n 不是/n
    还可以稍微优化一下,
    int main() {
    int m,n,ai,aj,bi,bj,ak,bk;
    while (scanf("%d%d",&m,&n)!=EOF) {
    ai = sqrt(m-1);
    bi = sqrt(n-1);
    aj = (m-ai*ai-1)>>1;
    bj = (n-bi*bi-1)>>1;
    ak = ((ai+1)*(ai+1)-m)>>1;
    bk = ((bi+1)*(bi+1)-n)>>1;
    printf("%dn",abs(ai-bi)+abs(aj-bj)+abs(ak-bk));
    }
    }

  3. 算法是程序的灵魂,算法分简单和复杂,如果不搞大数据类,程序员了解一下简单点的算法也是可以的,但是会算法的一定要会编程才行,程序员不一定要会算法,利于自己项目需要的可以简单了解。