1704 - 我是管理员

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 32 MB

ACM俱乐部可以为每一个学校提供一个属于自己的OJ(Online Judge,在线判题系统),假如你现在已经成为了自己学校的OJ管理员,你自然就拥有了两项基本权限:添加题目到OJ、删除OJ上已添加的题目。从此以后,要添要删你做主!

题目的添加规则如下:
题目序号从1000开始,每个题目的序号具有惟一性,且以1为单位逐渐增大。
如果某道题目被删除,则此题目序号作废,后面新添加的题目不会再次占用此序号。

现在给你若干添加删除操作,请你按照题目序号从小到大输出OJ中最终保留的题目信息。

题目输入

输入的第一行为一个正整数T(0<T<=10),表示有T组输入。
每组输入包含若干行,每行输入有三种情况:
(1)当如入“Add”,表示添加题目,然后空一格输入要添加的题目名称,题目名称的长度小于10。
(2)当输入“Delete”,表示删除题目,然后空一格输入要删除的题目名称。
(3)当输入“End”,表示本组输入结束,此行不作处理。
测试数据保证每组的第一行输入一定是Add操作,同时,删除的一定是当前OJ中存在的题目,题目名称只包含英文字母,且题目名称没有重名。

题目输出

对于每组输入,输出两行,第一行为最终OJ中保留的题目序号,从小到大排列,题目序号之间空一格。
第二行为上一行对应题目序号的题目名称,题目名称之间空一格。
如果最终OJ中没有保留的题目,则输出两个空行。

输入/输出样例

输入格式

2
Add acm
Add club
End
Add I
Add am
Add very
Delete very
Add happy
End

输出格式

1000 1001
acm club
1000 1001 1003
I am happy

C语言解答

#include <stdio.h>
#include <string.h>

struct Poj
{
    int num;
    char name[20];
} poj[100000];

int main()
{
    int l, i, t, n, j;
    char s[10], str[20];
    int flag;

    scanf("%d", &t);

    while (t--)
    {
        flag = l = 0; n = 1000;
        while(scanf("%s", s)&&strcmp("End", s)!=0)
        {
            scanf("%s", str);
            if(strcmp("Delete", s)==0)
            {
                for(i=0; i<l; i++)
                {
                    if(!strcmp(str, poj[i].name))
                    {
                        for(j=i; j<l-1; j++)
                        {
                            poj[j] = poj[j+1];
                        }
                        l--;
                        break;
                    }
                }
            }
            else
            {
                strcpy(poj[l].name, str);
                poj[l].num = n++;
                l++;
            }
        }
        for(i=0; i<l; i++)
        {
           flag = 1;
           if(i<l-1)
           {
                printf("%d ", poj[i].num);
            }
            else
                printf("%d\n", poj[i].num);
        }
        for(i=0; i<l; i++)
        {
           if(i<l-1)
           {
                printf("%s ", poj[i].name);
            }
            else
                printf("%s\n", poj[i].name);
        }

        if(!flag)
            printf("\n\n");
    }

    return 0;
}

C++解答

#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;

#define REPI(i, s, e)   for(int i = (s); i <= (e); i ++)
#define REPD(i, e, s)   for(int i = (e); i >= (s); i --)

const int DELTA = 1000;
const int MAXN  = 30 + 10;
const int MAXM  = 100000 + 10;

int idx;

char cmd[MAXN];
char name[MAXM][MAXN];


void Input(void) {
        idx = 0;
        while( scanf("%s", cmd) ) {
                if( !strcmp(cmd, "End") ) {
                        break;
                }
                if( !strcmp(cmd, "Add") ) {
                        scanf("%s", name[idx ++]);
                }
                else if( !strcmp(cmd, "Delete") ) {
                        scanf("%s", cmd);
                        REPI(i, 0, idx-1) {
                                if( !strcmp(cmd, name[i]) ) {
                                        strcpy(name[i], "NULL");
                                        break;
                                }
                        }
                }
        }
}

void Process(void) {
        int flag;

        flag = 0;
        REPI(i, 0, idx-1) {
                if( !strcmp(name[i], "NULL") ) {
                        continue;
                }
                if( flag ++ ) {
                        printf(" ");
                }
                printf("%d", i+DELTA);
        }
        printf("\n");

        flag = 0;
        REPI(i, 0, idx-1) {
                if( !strcmp(name[i], "NULL") ) {
                        continue;
                }
                if( flag ++ ) {
                        printf(" ");
                }
                printf("%s", name[i]);
        }
        printf("\n");
}

int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
        freopen("test.in", "r", stdin);
#endif
        int cas;
        scanf("%d", &cas);
        REPI(k, 1, cas) {
                Input();
                Process();
        }
        return 0;
}

Java解答

import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		while (n-- > 0) {
			String titles = "";
			String title = "";
			while (true) {
				String inputLine = scanner.nextLine();
				if (inputLine.startsWith("End")) {
					break;
				} else {
					if (inputLine.startsWith("Add")) {
						title = inputLine.replaceFirst("Add", "");
						title = title.trim();
						titles += "&&&" + title;
					} else if (inputLine.startsWith("Delete")) {
						title = inputLine.replaceFirst("Delete", "");
						title = title.trim();
						titles = titles.replaceFirst(title, "#DELETED#");
					}
				}
			}
			
			titles = titles.replaceFirst("&&&", "");

			boolean is1st = true;
			boolean isEmpty = true;
			
			int startNumber = 1000;
			String[] titleList = titles.split("&&&");
			int length = titleList.length;
			for (int i = 0; i < length; i++, startNumber++) {
				title = titleList[i].trim();

				if (title.contains("#DELETED#")) {
					continue;
				} else {
					isEmpty = false;
					if (is1st) System.out.print(startNumber);
					else System.out.print(" " + startNumber);
					is1st = false;
				}
			}
			System.out.println();
			
			is1st = true;
			isEmpty = true;
			for (int i = 0; i < length; i++) {
				title = titleList[i].trim();
				if (title.contains("#DELETED#")) {
					continue;
				} else {
					isEmpty = false;
					if (is1st) System.out.print(title);
					else System.out.print(" " + title);
					is1st = false;
				}
			}
			System.out.println();
		}
	}
	
}