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;
    }
    

  • 相关阅读:
    腾讯课堂目标2017高中数学联赛基础班-2作业题解答-1
    腾讯课堂目标2017初中数学联赛集训队作业题解答-1
    2016猿辅导初中数学竞赛基础特训营作业题
    Markdown的基本语法
    解决网络图片加载出现403错误
    深入理解JS引擎的执行机制
    vue中moment.js的使用
    js中字符串 stringObject 的 replace() 方法
    Object.keys()方法
    webpack的require.context()实现路由“去中心化”管理
  • 原文地址:https://www.cnblogs.com/NYNU-ACM/p/4237460.html
Copyright © 2011-2022 走看看