(HDUStep 1.2.5)The Seven Percent Solution(字符串的替换)

题目:

The Seven Percent Solution

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2637 Accepted Submission(s): 1562
 

Problem Description

Uniform Resource Identifiers (or URIs) are strings like http://icpc.baylor.edu/icpc/, mailto:[email protected], ftp://127.0.0.1/pub/linux, or even just readme.txt that are used to identify a resource, usually on the Internet or a local computer. Certain characters are reserved within URIs, and if a reserved character is part of an identifier then it must be percent-encoded by replacing it with a percent sign followed by two hexadecimal digits representing the ASCII code of the character. A table of seven reserved characters and their encodings is shown below. Your job is to write a program that can percent-encode a string of characters.

Character  Encoding
" " (space)  %20
"!" (exclamation point)  %21
"$" (dollar sign)  %24
"%" (percent sign)  %25
"(" (left parenthesis)  %28
")" (right parenthesis)  %29
"*" (asterisk)  %2a


Input

The input consists of one or more strings, each 1–79 characters long and on a line by itself, followed by a line containing only "#" that signals the end of the input. The character "#" is used only as an end-of-input marker and will not appear anywhere else in the input. A string may contain spaces, but not at the beginning or end of the string, and there will never be two or more consecutive spaces.


Output

For each input string, replace every occurrence of a reserved character in the table above by its percent-encoding, exactly as shown, and output the resulting string on a line by itself. Note that the percent-encoding for an asterisk is %2a (with a lowercase "a") rather than %2A (with an uppercase "A").


Sample Input

Happy Joy Joy!
http://icpc.baylor.edu/icpc/
plain_vanilla
(**)
?
the 7% solution
#


Sample Output

Happy%20Joy%20Joy%21
http://icpc.baylor.edu/icpc/
plain_vanilla
%28%2a%2a%29
?
the%207%25%20solution

 

Source

Mid-Central USA 2007


Recommend

teddy

题目分析:

按要求将字符串换成指定的字符串即可。因为用java比较快。所以使用java来实现

代码如下:

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		while(scanner.hasNext()){
			String str = scanner.nextLine();

			if(str.equals("#")){
				return ;
			}

			str = str.replace("%", "%25");//因为替换的结果有%,所以把%的替换放在前面
			str = str.replace(" ", "%20");
			str = str.replace("!", "%21");
			str = str.replace("$", "%24");
			str = str.replace("(", "%28");
			str = str.replace(")", "%29");
			str = str.replace("*", "%2a");

			System.out.println(str);
		}
	}
}
时间: 2024-08-04 13:49:20

(HDUStep 1.2.5)The Seven Percent Solution(字符串的替换)的相关文章

zoj 2932 The Seven Percent Solution

The Seven Percent Solution Time Limit: 2 Seconds      Memory Limit: 65536 KB Uniform Resource Identifiers (or URIs) are strings like http://icpc.baylor.edu/icpc/, mailto:[email protected], ftp://127.0.0.1/pub/linux, or even just readme.txt that are u

题目1.2.4 The Seven Percent Solution(C++)

Problem Description Uniform Resource Identifiers (or URIs) are strings like http://icpc.baylor.edu/icpc/, mailto:[email protected], ftp://127.0.0.1/pub/linux, or even just readme.txt that are used to identify a resource, usually on the Internet or a

POJ 3650 & ZJU 2932 & HDU 2719 The Seven Percent Solution(模拟)

题目链接: PKU:http://poj.org/problem?id=3650 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1931 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2719 Description Uniform Resource Identifiers (or URIs) are strings like http://icpc.baylor.edu

HDUOJ 2560 The Seven Percent Solution

#include<iostream> #include<stdlib.h> using namespace std; int main() { //第一个循环用于输入,遇到#停止 while (1) { //第二个循环用于单次输入的每个字符的判断 while (1) { char c; c = getchar(); //若遇到换行符,则结束此次输入 if (c == '\n' || c == EOF) { cout << endl; break; } //遇到#停止 i

【转】100本最棒的英文侦探小说

阅读计划2:100本最棒的英文侦探小说2010-08-15 13:21:28The Top 100 Crime Novels of All Time The Top 100 Crime Novels of All Time is a list published in book form in 1990 by the British-based Crime Writers' Association. Five years later, the Mystery Writers of America

美国100部推理小说排行榜

这份书单是美国推理作家协会(The Mystery Writers of America,简称MWA)票选出来的史上最经典的一百部推理小说排行榜.评选过程极为复杂,首先由推理协会选出本格派.硬汉派.惊悚派.间谍小说等推理小说十个主要流派的十个代表作家,再由这十个作家选出各流派的十部代表作品,然后由协会会员投票排出名次.所以这份书单不仅仅有总的一百强排行榜,还有各个不同流派的十大杰作排名,其中本格和硬汉两派由于名作数量远远超出其它各派,所以都各排了二十部作品. 后来美国出了一本书<The Crow

HustOJ - 1029

1 #include<iostream> 2 #include<string> 3 using namespace std; 4 int main() 5 { 6 int i=0; 7 string s; 8 while(getline(cin,s)) 9 { 10 if (s[0]=='#') break; 11 else 12 { 13 for (i=0;i<s.size();i++) 14 { 15 if (s[i]==' ') cout<<"%2

LeetCode:Word Pattern - 字符串模式匹配

1.题目名称 Word Pattern(字符串模式匹配) 2.题目地址 https://leetcode.com/problems/word-pattern/ 3.题目内容 英文:Given a pattern and a string str, find if str follows the same pattern. 中文:给出一组模式(pattern)和一个字符串(str),查看字符串是否与模式匹配 例如: pattern = "abba",str = "dog cat

不简单的SQL查询和排序语句

真不简单!! 一:使用select语句进行查询 语法: SELECT    <列名> FROM      <表名> [WHERE    <查询条件表达式>] [ORDER BY <排序的列名>[ASC或DESC]] eg1: SELECT               SCode,SName,SAddress FROM   Students WHERE               SSEX = 0 ORDER BY   SCode 二:查询所有列和行: eg: