从标准输入读取字符串,按照指定的两层分隔符切分成多对key-value,依次输出到标准输出中。
注意:仅输出key和value都为非空串的pair。
每一行有效输入为三列,列之间以' '分隔,第一列表示key_value_pairs_delimiter,第二列表示key_value_delimiter,第三列表示待切分的字符串。分隔符' '不会出现在列内容中。
输入样例:
# : a:3#b:8#c:9
先输出有效key-value对的个数(单独一行);再依次输出key-value对,每一对单独成行,以' ‘分隔key和value。
输出样例:
3
a 3
b 8
c 9
# : a:3#b:8#c:9
3 a 3 b 8 c 9
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String[] strings = sc.nextLine().split(" "); // # : a:3#b:8#c:9 // 注意对输入做校验 if (strings.length < 3) { System.out.println(0); } else { String a = strings[0], b = strings[1]; String[] splitString = strings[2].split(a); ArrayList<String> res = new ArrayList<>(); for (String s : splitString) { if (s.contains(b)) { res.add(s); } } if (!res.isEmpty()) { System.out.println(res.size()); for (String re : res) { System.out.println(re.replace(b, " ")); } } else System.out.println(0); } } }注意对输入数据做校验,要不然提示数组可能越界,通过率一直卡在 80%
import java.util.*; public class Main{ public static void main(String[] args){ Scanner input = new Scanner(System.in); String str = input.nextLine(); if(str.length()<4){ System.out.println(0); } else { char sc = str.charAt(0); char xc = str.charAt(2);//1. 先获取间隔符和关系符 String[] ans = str.substring(4).split(sc+"");//2. 用split(间隔符)得到分离后的字符串数组 int ansnum = ans.length; for(String s : ans){ if(!s.contains(xc+"")) ansnum--; } System.out.println(ansnum); if(ansnum>0){ for(String s : ans){ if(s.contains(xc+"")){//3. 用contains(关系符)检验各个字符串是否标准 System.out.println(s.replace(xc,' '));//4. 用replace(关系符, 空格)输出 } } } }} }
#include<iostream> #include<string> #include<vector> #include<sstream> #include<istream> using namespace std; int main() { char a, b; string c,p; cin >> a >> b; getline(cin, c); vector<string>s, q; stringstream ss(c); while (getline(ss, c, a)) //第一次分割 s.push_back(c); cout << s.size() << endl; for (int i = 0; i < s.size(); i++) { stringstream tmp(s[i]); while (getline(tmp, c, b)) //第二次分割 q.push_back(c); for (int i = 0; i < q.size(); i++) { //把空格去掉 stringstream tmpq(q[i]); while (getline(tmpq, c, ' ')) p += c; if (i != q.size()) cout << p << ' '; else cout << p; p = ""; } q.clear(); cout << endl; } s.clear(); }
using System; using System.Collections.Generic; namespace leetcode_sharp { class Program { static void Main(string[] args) { List<string> keylist = new List<string>(); List<string> valuelist = new List<string>(); string line = Console.ReadLine(); var spl = line.Split(" "); if(spl.Length!=3) { Console.WriteLine("0"); return; } var pair_sp = spl[0]; var kv_sp = spl[1]; var kvs = spl[2].Split(pair_sp); foreach (var item in kvs) { string[] t = item.Split(kv_sp); if (t.Length > 1) { keylist.Add(t[0]); valuelist.Add(t[1]); } } Console.WriteLine(keylist.Count); for(int i = 0; i < keylist.Count; i++) { Console.WriteLine("{0} {1}",keylist[i],valuelist[i]); } } } }暴力
#include<bits/stdc++.h> using namespace std; typedef pair<string,string> PII; int main() { string str; while(getline(cin,str)) { if(str.size()<=3) { cout<<0<<endl; continue; } string c1,c2; int p1=str.find(' ',0); c1=str.substr(0,p1); //分割键值对 int p2=str.find(' ',p1+1); c2=str.substr(p1+1,p2-p1-1); //分割k和v vector<string> key_val; int st=p2+1; while(st<str.size()) { int pos=str.find(c1,st); if(string::npos==pos) { key_val.push_back(str.substr(st,str.size()-st)); st=str.size(); } else{ key_val.push_back(str.substr(st,pos-st)); st=pos+c1.size(); } } vector<PII> ans; for(auto &s:key_val) { int pos=s.find(c2,0); if(pos==string::npos) continue; ans.push_back({s.substr(0,pos),s.substr(c2.size()+pos)}); } cout<<ans.size()<<endl; for(auto &p:ans) cout<<p.first<<" "<<p.second<<endl; } return 0; }
#include <iostream> #include <cstring> #include <vector> #include <string> using namespace std; int main() { vector<pair<string, string>> res; char *p, *p1, *p2; char a, b, str[1000] = { 0 }; cin >> a >> b >> str; p1 = str; while (p = strchr(p1, b)) { char tmp1[100] = { 0 }, tmp2[100] = { 0 }; p2 = p; while (*p2 != a && p2 != str) { p2--; } if (p2 != str) p2++; strncpy(tmp1, p2, p - p2); p2 = p; while (*p2 != a && *p2 != '\0') { p2++; } strncpy(tmp2, p + 1, p2 - p - 1); res.push_back(make_pair(string(tmp1), string(tmp2))); p1 = p + 1; } cout << res.size() << endl; for (auto elem : res) { cout << elem.first << ' ' << elem.second << endl; } return 0; }
public void my(){ Scanner scanner = new Scanner(System.in); String str = scanner.nextLine(); String[] chars = str.split(" "); //# String pairs_delimiter = chars[0]; //: char k_v_delimiter = chars[1].charAt(0); //a:3#b:8#c:9 String kv= chars[2]; String[] target = kv.split(pairs_delimiter); System.out.println(target.length); for(int i = 0;i < target.length;i++){ String s = target[i].replace(k_v_delimiter, ' '); System.out.println(s); } }
哥哥们为啥本地过了,牛客没过
importjava.util.*;publicclassMain{publicstaticvoidmain(String[] args){Scanner sc = newScanner(System.in);String s = sc.nextLine();intN = s.length();if(N < 5){System.out.println(0);return;}String[] ss = s.split(" ");String key_value_pairs_delimiter = ss[0];String key_value_delimiter = ss[1];String[] keyValues = ss[2].split(key_value_pairs_delimiter);intsize = 0;String[] res = newString[keyValues.length];for(inti = 0; i < keyValues.length; i++){String[] keyValue = keyValues[i].split(key_value_delimiter);if(keyValue.length == 2){res[size] = keyValue[0] + " "+ keyValue[1];size++;}}System.out.println(size);for(inti = 0; i < size; i++){System.out.println(res[i]);}}}
import java.util.Scanner; import java.util.Map; import java.util.HashMap; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); // # : a:5#bb:7#w:4 String input = s.nextLine(); String[] src = input.split(" "); if(src.length < 3){ System.out.println(0); return; } // 分隔符 String deco1 = src[0]; String deco2 = src[1]; String in = src[2]; String[] str = in.split(deco1); int count = 0; Map<String, String> result = new HashMap<String, String>(); for (int i = 0; i < str.length; i++) { String[] temp = str[i].split(deco2); if(temp.length<2){ continue; } if (!"".equals(temp[0]) && !"".equals(temp[1])) { count++; result.put(temp[0], temp[1]); } } System.out.println(count); for (String key : result.keySet()) { System.out.println(key + " " + result.get(key)); } } }
#include <iostream> (720)#include <sstream> #include <string> (765)#include <vector> using namespace std; int main() { char pair_delim, kv_delim; string str; cin >> pair_delim >> kv_delim >> str; stringstream ss(str); string pair; vector<string> res; unsigned cnt = 0; while (getline(ss, pair, pair_delim)) { string k_or_v, k_and_v; if (pair.find(kv_delim) == pair.npos) continue; stringstream ss2(pair); while (getline(ss2, k_or_v, kv_delim)) { k_and_v += k_or_v + " "; } res.push_back(k_and_v); ++cnt; k_and_v.clear(); } cout << cnt << endl; for (const string &s : res) cout << s << endl; return 0; }