ACM集训STL(1)学习杂记(二)——E题~H题

STL(1)

(E)Online Judge

很简单的一道题,再看自己的代码会发现一些没必要的地方。

Online Judge

Ignatius is building an Online Judge, now he has worked out all the problems except the Judge System. The system has to read data from correct output file and user's result file, then the system compare the two files. If the two files are absolutly same, then the Judge System return "Accepted", else if the only differences between the two files are spaces(' '), tabs('\t'), or enters('\n'), the Judge System should return "Presentation Error", else the system will return "Wrong Answer".
Given the data of correct output file and the data of user's result file, your task is to determine which result the Judge System will return.

Input

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case has two parts, the data of correct output file and the data of the user's result file. Both of them are starts with a single line contains a string "START" and end with a single line contains a string "END", these two strings are not the data. In other words, the data is between the two strings. The data will at most 5000 characters.

Output

For each test cases, you should output the the result Judge System should return.

Sample Input

4
START
1 + 2 = 3
END
START
1+2=3
END
START
1 + 2 = 3
END
START
1 + 2 = 3

END
START
1 + 2 = 3
END
START
1 + 2 = 4
END
START
1 + 2 = 3
END
START
1	+	2	=	3
END

Sample Output

Presentation Error
Presentation Error
Wrong Answer
Presentation Error

最终AC代码

#include <iostream>
#include <vector>
#include <cstring>
#include <string>
using namespace std;
bool ac(string i,string j)
{
    if (i==j) return true;
    else return false;
}
int main()
{
    int n;
    string m,s1,s2,t1,t2;
    cin>>n;
    cin.get();
    for(int i=0;i<n;++i)
    {
        string j;
        getline(cin,j);
        s1.clear();
        s2.clear();
        t1.clear();
        t2.clear();
        s1="";
        s2="";
        while(getline(cin,m))
        {
            if (m=="END") break;
            s1=s1+m+'\n';
        }
        getline(cin,j);
        while(getline(cin,m))
        {
            if (m=="END") break;
            s2=s2+m+'\n';
        }
        for(unsigned long long i=0;i<s1.size();i++)
        {
            if(s1[i]=='\t'||s1[i]==' '||s1[i]=='\n') continue;
            else t1.push_back(s1[i]);
        }
        for(unsigned long long i=0;i<s2.size();i++)
        {
            if(s2[i]=='\t'||s2[i]==' '||s2[i]=='\n') continue;
            else t2.push_back(s2[i]);
        }
        if(ac(s1,s2)) cout<<"Accepted"<<endl;
        else if(ac(t1,t2)) cout<<"Presentation Error"<<endl;
        else cout<<"Wrong Answer"<<endl;
    }
}

备注

唯一错的地方在于cin>>n;之后忘加cin.get();,cin是不会读取键盘缓存区的空白符的,之后直接用getline会读取一个换行符,用cin.get()把这个换行符读掉即可。getline会读取结束符(第三个参数,默认为换行符),但不会放入字符串,因此之后不用cin.get()。


(F)Write a simple HTML Browser

替换特定字符串,简单的模拟题。

Write a simple HTML Browser

If you ever tried to read a html document on a Macintosh, you know how hard it is if no Netscape is installed.
Now, who can forget to install a HTML browser? This is very easy because most of the times you don't need one on a MAC because there is a Acrobate Reader which is native to MAC. But if you ever need one, what do you do?
Your task is to write a small html-browser. It should only display the content of the input-file and knows only the html commands (tags) <br> which is a linebreak and <hr> which is a horizontal ruler. Then you should treat all tabulators, spaces and newlines as one space and display the resulting text with no more than 80 characters on a line.

Input

The input consists of a text you should display. This text consists of words and HTML tags separated by one or more spaces, tabulators or newlines.
A word is a sequence of letters, numbers and punctuation. For example, "abc,123" is one word, but "abc, 123" are two words, namely "abc," and "123". A word is always shorter than 81 characters and does not contain any '<' or '>'. All HTML tags are either <br> or <hr>.

Output

You should display the the resulting text using this rules:
  . If you read a word in the input and the resulting line does not get longer than 80 chars, print it, else print it on a new line.
  . If you read a <br> in the input, start a new line.
  . If you read a <hr> in the input, start a new line unless you already are at the beginning of a line, display 80 characters of '-' and start a new line (again).
The last line is ended by a newline character.

Sample Input

Hallo, dies ist eine 
ziemlich lange Zeile, die in Html
aber nicht umgebrochen wird.
<br>
Zwei <br> <br> produzieren zwei Newlines. 
Es gibt auch noch das tag <hr> was einen Trenner darstellt.
Zwei <hr> <hr> produzieren zwei Horizontal Rulers.
Achtung       mehrere Leerzeichen irritieren

Html genauso wenig wie


mehrere Leerzeilen.

Sample Output

Hallo, dies ist eine ziemlich lange Zeile, die in Html aber nicht umgebrochen
wird.
Zwei

produzieren zwei Newlines. Es gibt auch noch das tag
--------------------------------------------------------------------------------
was einen Trenner darstellt. Zwei
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
produzieren zwei Horizontal Rulers. Achtung mehrere Leerzeichen irritieren Html
genauso wenig wie mehrere Leerzeilen.

最终AC代码

#include <iostream>
#include <vector>
#include <cstring>
#include <string>
using namespace std;
int main()
{
    int z=0;
    char n='\n';
    string m,s="";
    while(cin>>m)
    {
        if(m=="<br>")
        {
            s=s+'\n';
            z=0;
            n='\n';
            continue;
        }
        if(m=="<hr>")
        {
            if(n!='\n')
            {
                s=s+'\n';
                n='\n';
            }
            for(int i=0;i<80;++i)
                s=s+"-";
            s=s+'\n';
            z=0;
            n='\n';
            continue;
        }
        if (n=='\n')
        {
            s=s+m;
            z=z+m.size();
            n=*m.rbegin();
            continue;
        }
        z=z+m.size()+1;
        n=*m.rbegin();
        if (z<=80)
        {
            s=s+" "+m;
            continue;
        }
        else
        {
            s=s+"\n"+m;
            z=m.size();
            n=*m.rbegin();
        }
    }
    cout<<s;
    if(n!='\n') cout<<'\n';
}

备注

一道比起WA更容易PE的题,理清思路就行了。


(G)Kaitou Kid - The Phantom Thief (1)

同样是替换字符串,这道题有三种不同的分隔符难度就增加了。最后还是用了笨办法一个个字符判断。似乎正则表达式可以更简单得解决这个问题,可惜不会。有空再学。

Kaitou Kid - The Phantom Thief (1)

Do you know Kaitou Kid? In the legend, Kaitou Kid is a master of disguise, and can take on the voice and form of anyone. He is not an evil person, but he is on the wrong side of the law. He's the very elusive phantom thief who never miss his prey although he always uses word puzzles to announce his targets before action.



You are the leader of a museum. Recently, you get several priceless jewels and plan to hold an exhibition. But at the moment, you receive Kid's word puzzle... Fortunately, It seems Kid doesn’t want to trouble you, and his puzzle is very easy. Just a few minutes, You have found the way to solve the puzzle:

(1) change 1 to 'A', 2 TO 'B',..,26 TO 'Z'
(2) change '#' to a blank
(3) ignore the '-' symbol, it just used to separate the numbers in the puzzle

Input

The first line of the input contains an integer C which means the number of test cases. Then C lines follow. Each line is a sentence of Kid’s word puzzle which is consisted of '0' ~ '9' , '-' and '#'. The length of each sentence is no longer than 10000.

Output

For each case, output the translated text.

Sample Input

4
9#23-9-12-12#19-20-5-1-12#1-20#12-5-1-19-20#15-14-5#10-5-23-5-12
1-14-4#12-5-1-22-5#20-8-5#13-21-19-5-21-13#9-14#20#13-9-14-21-20-5-19
1-6-20-5-18#20-8-5#15-16-5-14-9-14-7#15-6#20-8-5#5-24-8-9-2-9-20-9-15-14
7-15-15-4#12-21-3-11

Sample Output

I WILL STEAL AT LEAST ONE JEWEL
AND LEAVE THE MUSEUM IN T MINUTES
AFTER THE OPENING OF THE EXHIBITION
GOOD LUCK

最终AC代码

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
int main ()
{
    int n;
    vector<string> p,q;
    string m;
    while(cin>>n)
    {
        p.clear();
        q.clear();
        cin.get();
        for(int i=0;i<n;i++)
        {
            getline(cin,m);
            p.push_back(m);
            q.push_back("");
        }
        int sum=0;
        for(int i= 0;i<n;i++)
        {
            string m=p[i];
            for(int j=0;j<m.size();j++)
            {
                if((m[j]>='0'&&m[j]<='9'))
                {
                    sum=sum*10+m[j]-'0';
                    if(!(m[j+1]>='0'&&m[j+1]<='9'))
                    {
                        char c;
                        c='A'+sum-1;
                        q[i]=q[i]+c;
                        sum=0;
                    }
                }
                else if(m[j]=='#')
                    q[i]=q[i]+' ';
            }
        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<q[i].size();j++)
            {
                    string m=q[i];
                    cout<<m[j];
            }
            cout<<endl;
        }
    }
    return 0;
}

备注

同样是需要理清思路的题,实在想不清楚可以将过程写成函数。


(H)Time To Get Up

难点在于避免TLE。

Time To Get Up

Little Q's clock is alarming! It's time to get up now! However, after reading the time on the clock, Little Q lies down and starts sleeping again. Well, he has 55 alarms, and it's just the first one, he can continue sleeping for a while.

Little Q's clock uses a standard 7-segment LCD display for all digits, plus two small segments for the '':'', and shows all times in a 24-hour format. The '':'' segments are on at all times.



Your job is to help Little Q read the time shown on his clock.

Input

The first line of the input contains an integer T(1≤T≤1440)T(1≤T≤1440), denoting the number of test cases.
In each test case, there is an 7×217×21 ASCII image of the clock screen.
All digit segments are represented by two characters, and each colon segment is represented by one character. The character ''X'' indicates a segment that is on while ''.'' indicates anything else. See the sample input for details.

Output

For each test case, print a single line containing a string tt in the format of HH:MMHH:MM, where t(00:00≤t≤23:59)t(00:00≤t≤23:59), denoting the time shown on the clock.

Sample Input

1
.XX...XX.....XX...XX.
X..X....X......X.X..X
X..X....X.X....X.X..X
......XX.....XX...XX.
X..X.X....X....X.X..X
X..X.X.........X.X..X
.XX...XX.....XX...XX.

Sample Output

02:38

最终AC代码

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
inline int numb(int i,vector<string>& m)
{
    int a;
    if(m[3][1+i]!='X')
        if(m[6][1+i]=='X') a=0;
        else if(m[0][1+i]=='X') a=7;
        else a=1;
    else if(m[1][3+i]!='X')
        if(m[4][0+i]=='X') a=6;
        else a=5;
    else if(m[1][0+i]!='X')
        if(m[5][0+i]=='X') a=2;
        else a=3;
    else if(m[4][0+i]=='X') a=8;
        else if(m[0][1+i]=='X') a=9;
        else a=4;
    return a;
}
int main ()
{
    int n;
    cin>>n;
    cin.get();
    for(int i=0;i<n;++i)
    {
        vector<string> m;
        int a,b,c,d;
        for(int i=0;i<7;++i)
        {
            string n;
            getline(cin,n);
            m.push_back(n);
        }
        a= numb(0,m);
        b= numb(5,m);
        c= numb(12,m);
        d= numb(17,m);
        cout<<a<<b<<":"<<c<<d<<endl;
    }
}

备注

关键在于尽可能平均分配0-9的判断次数,观察数码管特征,尽量做到每一次判断都是一次二分。


至此stl(1)的8道题都写完了,其实回过头来看都是很简单的题,当初写的代码都有很多不足,不过这也正说明自己有所进步。stl有利有弊,本身非常便捷,很多代码用stl实现可以不用担心TLE,但有些时候用TLE反倒阻碍了自己的思路。我的解决方法也很简单,用自己最擅长的方式来实现代码就行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值