Strange keyboard (Blue Bridge Cup 2015)

Topic is this:

Strange keyboard

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 122  Solved: 24
[Submit][Status][Forum]

Description

 

Recently the scientists write in the papers report late at night, but do not blame the keyboard a series of problems, sometimes tap the keyboard out of two characters, sometimes the only one. Happens to the keyboard backspace key is broken.
You know there are scientists who are crotchety, he played an article, he is like a return to the original.
Scientists string satisfaction is such that, if the string has the same two adjacent characters on the elimination of this two characters to form a new string, if a new character string and also adjacent the same, and then removed. Until same no two adjacent characters so far.

 

 

Input

 

A string length is not greater than 2 * 10 ^ 5, comprising lowercase letters

 

 

Output

 

Scientists are satisfied with string.

 

 

Sample Input

reallazy

Sample Output

cuts

HINT

 

For the sample strings such changes, reallazy -> reaazy -> rezy.

 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Today's students when they were asked about this topic to 0.0.

Think that is a problem playing push the stack, the stack will write over 23333

(Note that reading problems, there may be only two identical letters adjacent case, do not think too much 233 333)

code show as below:

C:

 1 #include<stdio.h>
 2  
 3 #define MAX 200001
 4  
 5 struct Stack{
 6     char sz[MAX];
 7     long index;
 8 }S; 
 9  
10 int main()
11 {
12     S.index = 0;
13     char ip = 0;
14     while (EOF != scanf("%c", &ip))
15     {
16         if (ip != S.sz[S.index - 1])
17             S.sz[S.index++] = ip;
18         else
19             S.index--;
20     }
21     for (int i = 0; i < S.index; i++)
22     {
23         putchar(S.sz[i]);
24     }
25     return 0;
26 }

 

C++:

 1 #include<iostream> 
 2 #include <deque>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     ios::sync_with_stdio(false);
 9     deque<char> d;
10     char ip;
11     while (cin >> ip)
12     {
13         if (d.size() && ip == d.back())
14             d.pop_back();
15         else
16             d.push_back(ip);
17     }
18     while (d.size())
19     {
20         cout << d.front();
21         d.pop_front();
22     }
23     return 0;
24 }

 

 

Java:

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static void main(String[] args) {
 5         int a = 0;
 6         Scanner sc = new Scanner(System.in);
 7         char[] sz = new char[200001];
 8         int index = 0;
 9         int i = 0;
10         String str;
11         while(sc.hasNext())
12         {
13             str = sc.next();
14             while(index < str.length())
15             {
16                 if(i > 0 && str.charAt(index) == sz[i - 1])
17                 {
18                     i--;
19                 }
20                 else
21                 {
22                     sz[i++] = str.charAt(index);
23                 }
24                 index++;
25             }
26             for(int j = 0; j < i; j++)
27             {
28                 System.out.print(sz[j]);
29             }
30             i = 0;
31             index = 0;
32         }
33     }
34 }

Sui Suinian: java can only handle 65,534 characters, if only once will run out of WA. . . . .

Guess you like

Origin www.cnblogs.com/lost-legacy/p/12112008.html