zoukankan      html  css  js  c++  java
  • UVA11988.悲剧文本

    UVA11988:悲剧文本

    You’re typing a long text with a broken keyboard. Well it’s not so badly broken. The only problem with the keyboard is that sometimes the “home” key or the “end” key gets automatically pressed (internally). You’re not aware of this issue, since you’re focusing on the text and did not even turn on the monitor! After you finished typing, you can see a text on the screen (if you turn on the monitor). In Chinese, we can call it Beiju. Your task is to find the Beiju text.

    Input There are several test cases. Each test case is a single line containing at least one and at most 100,000 letters, underscores and two special characters ‘[’ and ‘]’. ‘[’ means the “Home” key is pressed internally, and ‘]’ means the “End” key is pressed internally. The input is terminated by end-of-file (EOF).

    Output For each case, print the Beiju text on the screen.

    Sample Input

    This_is_a_[Beiju]_text

    [[]][][]Happy_Birthday_to_Tsinghua_University

    Sample Output

    BeijuThis_is_a__text

    Happy_Birthday_to_Tsinghua_University

    一眼看过去,很明显是走链表,直接stl走一遍

    #include<cstdio>
    #include<list>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    using namespace std;
    list<char> s;
    string str;
    int main(){
    	int tmp;
    	
    	bool isHome = false;
    	list<char>::iterator point = s.begin();
    	while(getline(cin,str)/*scanf("%[^
    ]",&tmp)!=EOF*/){
    		for(int i = 0; i < str.length();i++){
    			char tmp = str[i];
    			if(isalnum(tmp)||tmp=='_'){
    				if(isHome){//insert
    					s.insert(point,tmp);
    				}else{
    					s.push_back(tmp);
    				}//push_back
    			}
    			else if(tmp=='['){//迭代器重置到begin 
    				point = s.begin();
    				isHome = true;
    				//ishome改为true 
    			}else if(tmp==']'){
    			point = s.end();
    			isHome = false;//迭代器重置end-1,ishome=false 
    		}
    	}
    		for(list<char>::iterator i=s.begin();i != s.end(); ++i){
    			printf("%c",*i);
    		} 
    		printf("
    ");
    		s.clear();
    
    	}
    }
    

    除了第一下读入字符串,然后循环把字符串读成char存入链表感觉有点费以外,一切还好。问题就是runtime error了(

    去网上搜了一下其他人的解题,发现stl能a掉,不过时间吃的很紧(有人100ms)。但我的直接runtime error了。

    先把坑留在这,用模拟链表的形式写吧。

    #include<cstdio>
    #include<cstring>
    
    const int maxn = 100005;
    int last, cur, next[maxn];
    char s[maxn];
    
    int main(){
    	while(scanf("%s",s+1)==1){
    		int n = strlen(s+1);
    		last = cur = 0;
    		next[0] = 0;
    		
    		for(int i = 1; i<=n; i++){
    			char ch = s[i];
    			if(ch == '[') cur = 0;
    			else if(ch == ']') cur = last;//abc[d]e
    			else{
    				next[i] = next[cur];
    				next[cur] = i;
    				if(cur == last) last = i;
    				cur = i;
    			}
    		}
    	for(int i = next[0];i != 0; i = next[i]) printf("%c",s[i]);
    	printf("
    ");
    	}
    }
    

    写完了,成功A掉。回过头来把stl写法的输入换成这个C风格字符串试试。

    ok了,不过还是超时了。[cin.getline和getline(cin,x)不一样,这就是我为什么runtime error了]

  • 相关阅读:
    java 九个预定义Class对象
    Android github上开源项目、酷炫的交互动画和视觉效果地址集合
    Axure多人协作
    尺取法
    android binder 机制二(client和普通server)
    11-11前的电商
    用minGW编译ffmpeg(供替换opencv中引用的ffmpeg库)
    设计模式_状态模式
    六句代码实现对文件按时间的重命名,
    在QML应用中实现threading多任务
  • 原文地址:https://www.cnblogs.com/ranbom/p/12957259.html
Copyright © 2011-2022 走看看