3 solutions
-
1
贪心:值得注意的是本题只用考虑子串一个字符和两个的情况,在贪心的思想下不会出现大于两个字符的子串,比如kkakk可以理解为k,kak,k,但在贪心的思想下会背优先分解为kk,a,kk,所以本题只用考虑前后项之间的关系
#include<iostream> #include<cstring> using namespace std; char a[200007]; int ans=1; int main(){ cin>>a; int n=strlen(a); for(int i=1;i<n;i++){ if(a[i]!=a[i-1])ans++; else a[i+1]='0'; } cout<<ans; return 0; }
-
0
#include <bits/stdc++.h> using namespace std; int main(){ string s,tmp; int num = 1; cin>>s; tmp = s.substr(0,1); for(int i = 1 ; i < s.length() ; i){ if(tmp == s.substr(i,1)){ if(i + 2 > s.length()){//查看是否还有2个字符空间 break; } tmp = s.substr(i,2); i += 2; num++; } else{ tmp = s.substr(i,1); i += 1; num++; } } cout<<num<<endl; return 0; }
- 1
Information
- ID
- 113
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 6
- Tags
- # Submissions
- 127
- Accepted
- 37
- Uploaded By