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了]

  • 相关阅读:
    深入剖析.NET运行机制
    在浏览器地址中加参数
    mysql 中文乱码
    今天在看一些面试题的时候遇到的一个关于strcmp()返回值的细节问题
    如何在学习过程中统一IDE的编码!
    通过文件方式,在文件内容开头写入字符串!
    Java编写圆的相关计算
    leetcode 上的Counting Bits 总结
    cocos2dx打包apk
    CEGUI0.8.4例子
  • 原文地址:https://www.cnblogs.com/ranbom/p/12957259.html
Copyright © 2011-2022 走看看