字符串的操作包括字符的查找,字符的定位,求子串,字符串比较,字符串的连接,求字符串的长度,字符串的替换等,现要求根据输入的字符串及其操作,输出相应的字符串。
输入:
输入包括一个用例,第一行为操作的次数,接下来为每次的操作,每次操作,第一行为操作要求,第二行为对应操作参数,第三行为要操作的字符串数,然后每行一个字符串。
search表示字符的查找,对应的参数是查找的字符,输出结果是第一次输出的位置,如果不存在,则输出0;index表示字符串的定位,对应参数是该位置的字符,如果不存在该位置,则输出NULL;substring表示求子串,对应的参数是开始和结束位置,如果结束位置超过最大,则只取到末尾;strcat表示字符串的连接,对应参数无,输出若干个字符串连接后的结果;strcompare表示字符串的比较,即比较两个字符串是否一样,对应参数无,如果一样,输出“YES”,否则输出“NO”;strlength表示求字符串的长度,输出对应字符串的长度;strreplace表示字符串的替换,对应参数有两行,第一行为要求替换的字符串,第二行为要求替换成的字符串,输出对应替换后的结果。
输出:
对每次操作,输出相应的结果。
样例输入:
7
search
b
2
abcdefg hi
aaeecc
index
2
3
654321
ab
b
substring
2 3
2
856987
ab
strcat
3
abc
a b
bbc
strcompare
Abc
abc
strlength
2
112233
abc
strreplace
ab
AB
2
12ab34ababc
AAbbab
样例输出:
2
0
5
b
NULL
56
b
abca bbbc
NO
6
3
12AB34ABABc
AAbbAB
思路:
刚开始就是函数,函数去解决掉它,但是错误了几遍之后就是发现不好了,然后就是不调用函数了,合成了一个main里面去了。
代码如下:
import java.util.Scanner;
public class comprehensiveString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int y = sc.nextInt(), number;
for (int i = 0; i < y; i++) {
String s = sc.next();
if (s.equals("search")) {
String s1, t;
s1 = sc.next();
number = sc.nextInt();
t = sc.nextLine();
while (number-- != 0) {
t = sc.nextLine();
int flag = 0;
for (int i1 = 0; i1 < t.length(); i1++)
if (t.charAt(i1) == s1.charAt(0)) {
flag = 1;
System.out.println(i1 + 1);
break;
}
if (flag == 0)
System.out.println(0);
}
} else if (s.equals("index")) {
int position = sc.nextInt();
number = sc.nextInt();
String s1 = sc.nextLine();/// 读掉回车
while (number-- != 0) {
s1 = sc.nextLine();
if (s1.length() < position)
System.out.println("NULL");
else
System.out.println(s1.charAt(position - 1));
}
} else if (s.equals("substring")) {
int start = sc.nextInt();
int end = sc.nextInt();
number = sc.nextInt();
String s1 = sc.nextLine();/// 读掉回车
while (number-- != 0) {
s1 = sc.nextLine();
for (int i1 = start - 1; i1 < end && i1 < s1.length(); i1++)
System.out.print(s1.charAt(i1));
System.out.println();
}
} else if (s.equals("strcat")) {
number = sc.nextInt();
String str = sc.nextLine();/// 读掉回车
str = "";
while (number-- != 0)
str = str + sc.nextLine();
System.out.println(str);
} else if (s.equals("strcompare")) {
String str1, str;
str = sc.nextLine();
str = sc.nextLine();
str1 = sc.nextLine();
if (str.equals(str1))
System.out.println("YES");
else
System.out.println("NO");
} else if (s.equals("strlength")) {
number = sc.nextInt();
String str = sc.nextLine();/// 读掉回车
str = "";
while (number-- != 0) {
str = sc.nextLine();
System.out.println(str.length());
}
} else if (s.equals("strreplace")) {
String str1, str, str2;
str = sc.nextLine();
str = sc.nextLine();
str1 = sc.nextLine();
number = sc.nextInt();
str2 = sc.nextLine();/// 读掉回车
while (number-- != 0) {
str2 = sc.nextLine();
int len = str2.length();
int j;
for (int i1 = 0; i1 < len; i1++) {
for (j = 0; j < str.length() && j < len; j++)
if (str2.charAt(i1 + j) != str.charAt(j))
break;
if (j == str.length()) {
i1 = i1 + str.length() - 1;
System.out.print(str1);
continue;
}
System.out.print(str2.charAt(i1));
}
System.out.println();
}
}
}
}
}
相关函数的调用但是超时间代码如下:
import java.util.Scanner;
public class comprehensiveString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
for (int i = 0; i < number; i++) {
String s = sc.next();
if (s.equals("search"))
search();
else if (s.equals("index"))
index();
else if (s.equals("substring"))
substring();
else if (s.equals("strcat"))
strcat();
else if (s.equals("strcompare"))
strcompare();
else if (s.equals("strlength"))
strlength();
else if (s.equals("strreplace"))
strreplace();
}
}
public static void strcat() {
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
String str = sc.nextLine();/// 读掉回车
str = "";
while (number-- != 0)
str = str + sc.nextLine();
System.out.println(str);
}
public static void strlength() {
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
String str = sc.nextLine();/// 读掉回车
str = "";
while (number-- != 0) {
str = sc.nextLine();
System.out.println(str.length());
}
}
public static void strcompare() {
Scanner sc = new Scanner(System.in);
String str1, str;
str = sc.nextLine();
str1 = sc.nextLine();
if (str.equals(str1))
System.out.println("YES");
else
System.out.println("NO");
}
public static void strreplace() {
Scanner sc = new Scanner(System.in);
String str1, str, str2;
str = sc.nextLine();
str1 = sc.nextLine();
int number = sc.nextInt();
str2 = sc.nextLine();/// 读掉回车
while (number-- != 0) {
str2 = sc.nextLine();
int len = str2.length();
int j;
for (int i = 0; i < len; i++) {
for (j = 0; j < str.length() && j < len; j++)
if (str2.charAt(i + j) != str.charAt(j))
break;
if (j == str.length()) {
i = i + str.length()-1;
System.out.print(str1);
continue;
}
System.out.print(str2.charAt(i));
}
System.out.println();
}
}
public static void index() {
Scanner sc = new Scanner(System.in);
int position = sc.nextInt();
int number = sc.nextInt();
String s = sc.nextLine();/// 读掉回车
while (number-- != 0) {
s = sc.nextLine();
if (s.length() < position)
System.out.println("NULL");
else
System.out.println(s.charAt(position - 1));
}
}
public static void substring() {
Scanner sc = new Scanner(System.in);
int start = sc.nextInt();
int end = sc.nextInt();
int number = sc.nextInt();
String s = sc.nextLine();/// 读掉回车
while (number-- != 0) {
s = sc.nextLine();
for (int i = start - 1; i < end && i < s.length(); i++)
System.out.print(s.charAt(i));
System.out.println();
}
}
public static void search() {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine(), t;
int number = sc.nextInt();
t = sc.nextLine();
while (number-- != 0) {
t = sc.nextLine();
int flag = 0;
for (int i = 0; i < t.length(); i++)
if (t.charAt(i) == s.charAt(0)) {
flag = 1;
System.out.println(i + 1);
break;
}
if (flag == 0)
System.out.println(0);
}
}
}