string
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
A string consisting of lowercase letters,two adjacent character can be combined if and only if this two character are different.And the combined character is the latter one. (such as "ac" can be combined, and the result is "a").
Now your work is to combined the character as more as possible so that finally the string can be shortest.
Input
A string consisting of lowercase letters. (1≤length≤200000)
Output
a positive integer x means the finally length of string.
Sample input and output
Sample Input | Sample Output |
---|---|
abcd | 1 |
aac | 2 |
分析:贪心求解,贪心策略:从后向前进行贪心
AC代码:
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=200000+10;
char s[maxn];
int d[maxn];
int main(){
while(scanf("%s",s)==1){
memset(d,0,sizeof(d));
int len=strlen(s);
d[len-1]=1;
for(int i=len-2;i>=0;i--){
if(s[i]==s[i+1])d[i]=d[i+1]+1;
else d[i]=1;
}
printf("%d\n",d[0]);
}
return 0;
}