codeforces_600A. Extract Numbers(字符串处理)
来源:互联网 发布:数据的收集与整理 编辑:程序博客网 时间:2024/06/10 03:52
You are given string s. Let's call word any largest sequence of consecutive symbols without symbols ',' (comma) and ';' (semicolon). For example, there are four words in string "aba,123;1a;0": "aba", "123", "1a", "0". A word can be empty: for example, the strings=";;" contains three empty words separated by ';'.
You should find all words in the given string that are nonnegative INTEGER numbers without leading zeroes and build by them new string a. String a should contain all words that are numbers separating them by ',' (the order of numbers should remain the same as in the string s). By all other words you should build string b in the same way (the order of numbers should remain the same as in the string s).
Here strings "101", "0" are INTEGER numbers, but "01" and "1.0" are not.
For example, for the string aba,123;1a;0 the string a would be equal to "123,0" and string b would be equal to "aba,1a".
The only line of input contains the string s (1 ≤ |s| ≤ 105). The string contains only symbols '.' (ASCII 46), ',' (ASCII 44), ';' (ASCII 59), digits, lowercase and uppercase latin letters.
Print the string a to the first line and string b to the second line. Each string should be surrounded by quotes (ASCII 34).
If there are no words that are numbers print dash (ASCII 45) on the first line. If all words are numbers print dash on the second line.
aba,123;1a;0
"123,0""aba,1a"
1;;01,a0,
"1"",01,a0,"
1
"1"-
a
-"a"
In the second example the string s contains five words: "1", "", "01", "a0", "".
对我来说有点恶心的模拟题,反正我是wa了两次,做了挺久的,而且代码时间复杂度很大,后来发现这道题用STL会很方便,
或者用上void *memcpy(void *dest, const void *src, size_t n);(从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中)也会减少代码量。。。。。
好吧,还是要多做。。。。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <map>#include <string>#include <algorithm>#define Si(a) scanf("%d",&a)#define Sl(a) scanf("%lld",&a)#define Sd(a) scanf("%lf",&a)#define Ss(a) scanf("%s",a)#define Pi(a) printf("%d\n",(a))#define Pl(a) printf("%lld\n",(a))#define Pd(a) printf("%lf\n",(a))#define Ps(a) printf("%s\n",(a))#define W(a) while(a--)#define mem(a,b) memset(a,(b),sizeof(a))#define inf 0x3f3f3f3f#define maxn 1000010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;char s[100010];char a[100010];char b[100010];int l_a=0,l_b=0;int is_int(char ch){ if(ch>='0'&&ch<='9')return 1; else return 0;}int is_a(char *c,int l,int r){ int i,j; if(c[l]=='0'&&(c[l+1]!=','&&c[l+1]!=';'&&c[l+1]!='\0'))return 0; for(i=l; i<=r; i++) { if(!is_int(c[i]))return 0; } //printf("l:%d\n",l); for(i=l; i<=r; i++) { a[l_a]=c[i]; l_a++; } a[l_a]=','; l_a++; return 1;}int load_b(char *c,int l,int r){ int i; if(c[l]==';'||c[l]==',') { b[l_b]=','; l_b++; return 1; } for(i=l; i<=r; i++) { b[l_b]=c[i]; l_b++; } b[l_b]=','; l_b++;}int main(){ gets(s); int len=strlen(s); s[len]=','; int i,j,l=0; for(i=0; s[i]!='\0'; i++) { if((s[i]==','||s[i]==';')&&i==0) { b[l_b]=','; l_b++; l++; continue; } if(s[l]==','||s[l]==';') { load_b(s,l,l); l++; if(s[i+1]!=','&&s[i+1]!=';') i++; continue; } if((s[i]==','||s[i]==';')) { int first=l; int last=i-1; //printf("%c %c\n",s[first],s[last]); if(!is_a(s,first,last)) { load_b(s,first,last); } l=i+1; } } if(!is_a(s,l,i)) { load_b(s,l,i); } a[l_a-1]='\0'; b[l_b-1]='\0'; if(strlen(a)==0) { printf("-\n"); } else { printf("%c%s%c\n",'"',a,'"'); } if(strlen(b)==0) { printf("-\n"); } else { printf("%c",'"'); for(i=0; i<strlen(b)-1; i++) { printf("%c",b[i]); } printf("%c\n",'"'); } return 0;}
补充上islava大神的漂亮代码
#include <bits/stdc++.h>#define files(x) freopen(x ".in", "r", stdin); freopen(x ".out", "w", stdout);#define all(a) (a).begin(),(a).end()#define ld long double#define ll long long#define sqr(a) (a)*(a)#define mp make_pair#define pb push_back#define x first#define y second#define inf (int)1e9#define pi pair<int,int>#define y1 fdfsusing namespace std;string s,t;vector<string>a,b;bool f(string s){ if(s.empty()) return 0; if(s.size()==1&&s=="0") return 1; if(s[0]=='0') return 0; if(!isdigit(s[0])) return 0; for(int i=0;i<s.size();++i) if(!isdigit(s[i])) return 0; return 1;}int main(){ #ifndef LOCAL //files(""); #endif ios_base::sync_with_stdio(0); cin>>s; for(int i=0;i<s.size();++i) { if(s[i]!=','&&s[i]!=';') { t.pb(s[i]); }else{ if(f(t)) a.pb(t); else b.pb(t); t.clear(); } } if(f(t)) a.pb(t); else b.pb(t); if(a.size()) cout<<'"'; for(int i=0;i+1<a.size();++i) cout<<a[i]<<','; if(a.size()) cout<<a.back()<<'"';else cout<<"-"; cout<<"\n"; a=b; if(a.size()) cout<<'"'; for(int i=0;i+1<a.size();++i) cout<<a[i]<<','; if(a.size()) cout<<a.back()<<'"';else cout<<"-"; cout<<"\n";}
- codeforces_600A. Extract Numbers(字符串处理)
- codeforces 600 A. Extract Numbers(字符串模拟)
- Extract Numbers
- codeforces A.Extract Numbers
- Educational Codeforces Round 2 A. Extract Numbers(模拟)
- 【Educational Codeforces Round 2A】【模拟】Extract Numbers 分离数串和字符串
- POJ 1016 Numbers That Count(字符串处理)
- poj 1504 Adding Reversed Numbers(简单字符串的处理)
- extract 函数处理时间
- [字符串处理]PAT1023 Have Fun with Numbers
- 【leetcode 字符串处理】Compare Version Numbers
- 1120. Friend Numbers (20)[字符串处理]
- 两个文件字符串小程序(CN_IPv4 extract)
- 处理概括关系之六 :Extract Subclass(提炼子类)
- 处理概括关系之八 :Extract Interface(提炼接口)
- Codeforces 600A Extract Numbers 【模拟】
- CodeForces 600A - Extract Numbers (模拟题)
- Educational Codeforces Round 2 A. Extract Numbers
- 零星小记备忘,浏览器差异
- JAVA实现字符表达式的计算的实现(一)--逆波兰式的实现
- AutoLayout——何为intrinsic content size
- VMWare中CentOS 6.5_x64迷你版安装(NAT方式)
- 单例模式
- codeforces_600A. Extract Numbers(字符串处理)
- 第二次上机作业
- 普通java类(排序)实现Comparable接口重写CompareTo方法
- 5.Servlet生成多位随机的验证码
- Linux内核协议栈(11)路由系统
- oracle中的char、varchar、varchar2、nvarchar、nvarchar2区别
- 构造MaxTree
- 【转】使用typedef定义函数指针
- 总结二