• 字符串提取问题


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

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

  • 相关阅读:
    编译gcc报错make[3]: Leaving directory `/usr/local/src/gcc-7.4.0/build/gcc' make[2]: *** [all-stage1-gcc] Error 2 处理
    ERROR 1176 (42000): Key 'XXX' doesn't exist in table 'XXX'报错处理
    /lib64/libc.so.6: version `GLIBC_2.18' not found报错解决
    Centos7上pkg-config的安装
    ERROR: Error in Log_event::read_log_event(): 'Found invalid event in binary log', data_len: 31, event_type: 35报错处理
    MySQL5.7主从复制slave报Last_Errno: 1146错误解决
    详述 hosts 文件的作用及修改 hosts 文件的方法
    Java Decompiler(Java反编译工具)
    使用Charles代理工具导致电脑无法正常访问网站(您的连接不是私密连接)
    阿里云服务器Svn-Server无法连接,阿里云服务器SVNServer配置
  • 原文地址:https://www.cnblogs.com/NYNU-ACM/p/4237460.html
Copyright © 2020-2023  润新知