题目链接:https://hihocoder.com/problemset/problem/1871
思路:满满的细节满满的坑,尤其是 123df123 居然也要算成123123 的时候真是惊呆了,我的做法是将所有字符串连起来,如果上一个最后是数字且下一个第一个是数字就不加空格,否则加一个空格,然后记录每个字符属于第几行的,最后遍历一遍合成的串,每个串check一下,记入答案。
终于的AC代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn = 5e5 + 50; 4 int ans[maxn]; 5 bool check(string &s){ 6 if(s.size() == 1 && s[0] == '0') return true; 7 if(s[0] >= '1' && s[0] <= '9'){} 8 else return false; 9 if(s.back() >= '0' && s.back() <= '9'){} 10 else return false; 11 int len = s.size(); 12 string x = ""; 13 for(int i = 0;i < len;i++){ 14 if(s[i] >= '0'&& s[i] <= '9') 15 x += s[i]; 16 } 17 s = x; 18 return true; 19 } 20 int pos[maxn]; 21 vector<string> v; 22 int main() 23 { 24 string a; 25 string s; 26 int cnt = 1; 27 bool last = false; 28 while(getline(cin,a)) 29 { 30 int n = a.size(); 31 if(a[0] >= '0' && a[0] <= '9' && last) s += a; 32 else s += ' ' + a; 33 pos[cnt++] = s.size(); 34 if(a[n - 1] >= '0' && a[n - 1] <= '9') last = true; 35 else last = false; 36 } 37 int n = cnt; 38 cnt = 1; 39 for(int i = 0;i < s.size();i++) 40 { 41 while(i >= pos[cnt]) cnt++; 42 string now = ""; 43 int j; 44 for(j = i;s[j] != ' ' && j < s.size();j++) 45 { 46 now += s[j]; 47 } 48 i = j; 49 if(check(now)) v.push_back(now), ans[cnt]++; 50 } 51 for(int i = 0;i < v.size();i++){ 52 cout << v[i]; 53 if(i == v.size() - 1) cout << endl; 54 else cout << " "; 55 } 56 for(int i = 1;i < n;i++) 57 { 58 cout << ans[i] << endl; 59 } 60 return 0; 61 }