zoukankan      html  css  js  c++  java
  • 字符串提取问题

    如何提取一个字符串中需要的片段:

    strchr 返回找到匹配位置的指针

    getchar()    什么字符都吸收

    Problem I

    Automatic Poetry

    Input: standard input

    Output: standard output

    Time Limit: 2 seconds

    Memory Limit: 32 MB

    “Oh God”, Lara Croft exclaims, “it’s one of these dumb riddles again!”

    In Tomb Raider XIV, Lara is, as ever, gunning her way through ancient Egyptian pyramids, prehistoric caves and medival hallways. Now she is standing in front of some important Germanic looking doorway and has to solve a linguistic riddle to pass. As usual, the riddle is not very intellectually challenging.

    This time, the riddle involves poems containing a “Schuttelreim”. An example of a Schuttelreim is the following short poem:

    Ein Kind halt seinen Schnabel nur,

    wenn es hangt an der Nabelschnur.        

    /*German contestants please forgive me. I had to modify something as they were not appearing correctly in plain text format*/

    A Schuttelreim seems to be a typical German invention. The funny thing about this strange type of poetry is that if somebody gives you the first line and the beginning of the second one, you can complete the poem yourself. Well, even a computer can do that, and your task is to write a program which completes them automatically. This will help Lara concentrate on the “action” part of Tomb Raider and not on the “intellectual” part.

    Input

    The input will begin with a line containing a single number n. After this line follow n pairs of lines containing Schuttelreims. The first line of each pair will be of the form

    s1<s2>s3<s4>s5

    where the si are possibly empty, strings of lowercase characters or blanks. The second line will be a string of lowercase characters or blanks ending with three dots “...”. Lines will we at most 100 characters long.

    Output

    For each pair of Schuttelreim lines l1 and l2 you are to output two lines c1 and c2 in the following way: c1 is the same as l1 only that the bracket marks “<” and “>” are removed. Line c2 is the same as l2 , except that instead of the three dots the string s4s3s2s5 should appear.

    Sample Input

    3

    ein kind haelt seinen <schn>abel <n>ur

    wenn es haengt an der ...

    weil wir zu spaet zur <>oma <k>amen

    verpassten wir das ...

    <d>u <b>ist

    ...

    Sample Output

    ein kind haelt seinen schnabel nur

    wenn es haengt an der nabel schnur

    weil wir zu spaet zur oma kamen

    verpassten wir das koma amen

    du bist

    bu dist

    解法如下:

    #include<iostream>
    #include<stdio.h>
    #include<string>
    #include<string.h>
    using namespace std ;
    int main()	{
    	int n ; 
    	cin >> n ;
    	getchar() ;
    	while(n--)	{
    		char s1[105] , s2[105] ;
    		char ss1[105] , ss2[105] , ss3[105] ,ss4[105] ;
    		gets(s1) ;
    		gets(s2) ;
    		int len = strlen(s1) ;
    		char *s3 = strchr(s1,'<') ;
    		char *s4 = strchr(s1,'>') ;
    		int i = 0 ;
    		for(++s3;s3 != s4;s3++,i++)
    			ss1[i] = *s3 ;
    		ss1[i] = '' ;
    		i = 0 ;
    		for(++s4 ; *s4 != '<';s4++,i++)
    			ss3[i] = *s4 ;
    		ss3[i] = '' ;
    		s3 = strchr(s3,'<') ;
    		s4 = strchr(s3,'>') ;
    		i = 0 ;
    		for(++s3;s3 != s4;s3++,i++)
    			ss2[i] = *s3 ;
    		ss2[i] = '' ;
    		i = 0 ;
    		for(++s4 ; *s4 != s1[len] ;s4++,i++)
    			ss4[i] = *s4 ;
    		ss4[i] = '' ;
    		for(i = 0 ; i < len ;i++)	{
    			if(s1[i] == '<' || s1[i] == '>')
    				continue ;
    			cout << s1[i] ;
    		}
    		cout<< endl ;
    		for(i = 0 ; s2[i] != '.' ; i++)
    			cout<< s2[i] ;
    		cout << ss2 << ss3  << ss1 << ss4 << endl ;
    	}
    	return 0 ;
    }
    

    另一种解法,在输入时提取:

    #include <stdio.h>
    #include <string.h>
    #define MAXN 110
    void getss(char s[]);
    
    void getss(char s[])
    {
    	int i;
    	for(i=0; i<MAXN; i++)
    	{
    		if((s[i] = getchar()) == '<' || s[i] == '>' || s[i] == '
    '){    // s[i] = getchar() 相当于 cin >> s[i] 
    			s[i] = '';
    			break;
    		}
    	}	
    }
    int main()
    {
    	int n;
    	char s1[MAXN],s2[MAXN],s3[MAXN],s4[MAXN],s5[MAXN],c,line[MAXN];
    	scanf("%d",&n);
    	getchar();
    	while(n--)
    	{		
    		getss(s1);
    		getss(s2);
    		getss(s3);
    		getss(s4);
    		getss(s5);
    		gets(line);
    		line[strlen(line) - 3] = '';
    		printf("%s%s%s%s%s
    ",s1,s2,s3,s4,s5);
    		printf("%s%s%s%s%s
    ",line,s4,s3,s2,s5);
    	}
    	return 0;
    }
    

  • 相关阅读:
    sys:1: RuntimeWarning: coroutine 'Launcher.killChrome' was never awaited
    python 引用对象相等,还是用list.extend()
    python 3.7.4 垃圾,一整天安装那个mitmproxy ,不行,卡在一个破库叫urwid ,说是os.path()为空,换3.8.2安装成功,垃圾3.7.4迟早要完
    python @staticmethod 注解,静态方法,可以省略类里那个self参数
    spring boot 记一次花了两天还是没有解决的奇怪bug(失去所有响应,post不到了,啥反应也没有了)
    Yum常用命令
    Centos安装与配置
    遍历hashmap的6种方法
    Java定时调度
    ElasticSearch的应用
  • 原文地址:https://www.cnblogs.com/scottding/p/3652748.html
Copyright © 2011-2022 走看看