Character c : s.toCharArray()
时间: 2024-05-25 18:16:32 浏览: 17
As an AI language model, I can explain that "s.toCharArray()" is a method in Java programming language that converts a string "s" into an array of characters. The method returns a new character array that contains all the characters in the string. The resulting character array can be used for various string manipulation tasks, such as searching, sorting, or modifying the characters in the string. The method is often used in combination with other string manipulation methods to perform various operations on strings.
相关问题
import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; import java.util.Stack; public class Assignment3 { public static boolean isMatched(String input) { Stack<Character> stack = new Stack<>(); for (char c : input.toCharArray()) { if (c == '{' || c == '[' || c == '(') { stack.push(c); } else if (c == '}' || c == ']' || c == ')') { if (stack.isEmpty()) { return false; } char top = stack.pop(); if ((top == '{' && c != '}') || (top == '[' && c != ']') || (top == '(' && c != ')')) { return false; } } } return stack.isEmpty(); } public static void main(String[] args) throws FileNotFoundException { File file=new File("C:\Users\DELL\Desktop\Chapter2\ADT\input_a3 (1).txt"); File file1=new File("C:\Users\DELL\Desktop\Chapter2\ADT\output_a3.txt"); Scanner sc1=new Scanner(file1); Scanner sc=new Scanner(file); PrintWriter writer = new PrintWriter(file1); while (sc.hasNextLine()) { String input = sc.nextLine(); if (isMatched(input)) { writer.printf("%s Valid \n", input); } else { writer.printf("%s inValid\n", input); } } writer.close(); sc.close(); sc1.close(); } }的ReadMe.txt
这段代码是一个用 Java 编写的括号匹配检查程序,它会读取一个文件中的多行文本,对每一行文本进行括号匹配检查,并将结果输出到另一个文件中。如果一行文本中的括号匹配正确,程序会输出 "Valid",否则会输出 "inValid"。程序使用了一个栈来实现括号匹配检查,具体实现可以参考 isMatched 方法的代码。在程序运行时,需要提供两个参数,第一个参数是输入文件的路径,第二个参数是输出文件的路径。
import java.util.LinkedList; import java.util.Scanner; import java.util.TreeSet; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); System.out.println(getResult(s)); } public static String getResult(String s) { StringBuilder sb = new StringBuilder(); LinkedList<TreeSet<Character>> eqs = new LinkedList<>(); boolean isOpen = false; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == '(') { isOpen = true; eqs.add(new TreeSet<>()); } else if (c == ')') { isOpen = false; if (eqs.getLast().size() == 0) eqs.removeLast(); } else { if (!isOpen) sb.append(c); else eqs.getLast().add(c); } } outer: while (true) { for (int i = 0; i < eqs.size(); i++) { for (int j = i + 1; j < eqs.size(); j++) { if (canCombine(eqs.get(i), eqs.get(j))) { eqs.get(i).addAll(eqs.get(j)); eqs.remove(j); continue outer; } } } break; } char[] cArr = sb.toString().toCharArray(); for (TreeSet<Character> eq : eqs) { Character t = eq.first(); for (int i = 0; i < cArr.length; i++) { if (eq.contains(cArr[i])) cArr[i] = t; } } String ans = new String(cArr); return ans.length() == 0 ? "0" : ans; } public static boolean canCombine(TreeSet<Character> set1, TreeSet<Character> set2) { for (char c = 'a'; c <= 'z'; c++) { char uc = (char) (c - 32); if ((set1.contains(c) || set1.contains(uc)) && (set2.contains(c) || set2.contains(uc))) { return true; } } return false; } }翻译成C++
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
string getResult(string s) {
string ans;
vector<set<char>> eqs;
bool isOpen = false;
for (int i = 0; i < s.length(); i++) {
char c = s[i];
if (c == '(') {
isOpen = true;
eqs.push_back(set<char>());
} else if (c == ')') {
isOpen = false;
if (eqs.back().empty()) eqs.pop_back();
} else {
if (!isOpen) ans += c;
else eqs.back().insert(c);
}
}
while (true) {
bool combined = false;
for (int i = 0; i < eqs.size(); i++) {
for (int j = i + 1; j < eqs.size(); j++) {
if (canCombine(eqs[i], eqs[j])) {
eqs[i].insert(eqs[j].begin(), eqs[j].end());
eqs.erase(eqs.begin() + j);
combined = true;
break;
}
}
if (combined) break;
}
if (!combined) break;
}
for (char& c : ans) {
for (const set<char>& eq : eqs) {
char t = *eq.begin();
if (eq.count(c)) c = t;
}
}
return ans.empty() ? "0" : ans;
}
bool canCombine(const set<char>& set1, const set<char>& set2) {
for (char c = 'a'; c <= 'z'; c++) {
char uc = toupper(c);
if ((set1.count(c) || set1.count(uc)) && (set2.count(c) || set2.count(uc))) {
return true;
}
}
return false;
}
int main() {
string s;
getline(cin, s);
cout << getResult(s) << endl;
return 0;
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)