String reduction (poj 3401
2024-10-07 11:46:41
String reduction
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1360 | Accepted: 447 |
Description
There is a string of characters 'a' and 'b' with the length of no more than 255 characters. You can perform the substring reduction on the initial string in the following way: a substring "a*a" or "b*b" (where * (asterisk) denotes any character) can be reduces to a substring "*".
The task is to achieve a string of minimal possible length after several substring reductions.
Input
The input contains the initial string.
Output
The output contains a single line with the minimal possible length.
Sample Input
aab
Sample Output
3
Source
Northeastern Europe 2001, Western Subregion
题意:在一个串中形如a*a 或b*b可以变换为a或b,问变换之后最短长度。
由于a*a, b*b之类的字符可以匹配任意字符,那么我们只要从前
向后扫描,如果有相隔的两个字符相同,由于这三个字符能够匹配a,b任意字符,那么我们就可以让这种匹配一直进行
下去,从而将字符串不断的reduction ,只要注意字符串长度的奇偶性就可以了,如此我们可以得到一个很简单的方法
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue> using namespace std; #define N 1100
#define INF 0xfffffff
#define eps 1e-8 int main()
{
char str[N]; scanf("%s", str);
int ans, len;
len = ans = strlen(str); for(int i = ; i + < len; i++)
{
if(str[i] == str[i+])
{
if(len % )
ans = ;
else
ans = ;
}
}
printf("%d\n", ans);
return ;
}
最新文章
- vue.js 第五课
- Powershell 十个常见任务
- ORA-27101 ORACLE not available
- javascript练习-扑克牌
- Hive性能优化
- Go语言实现HashSet
- 杭电1097-A hard puzzle
- 微博输入相关js 代码
- oracle 随笔
- python3.6+linux服务器+django1.11连接MYSQL数据库
- JavaScript学习之自动轮播图片
- [原]Webpack 3 + AngularJS1.* + Bootstrap 4 + Mapbox-gl
- luogu P3704 [SDOI2017]数字表格
- 4 Django应用 第3部分(视图部分)
- 【WEB2.0】 网页调用QQ聊天(PC+M站)
- javascript原生bind方法详解
- fscanf和feof的组合使用
- 配置Linux接收H3C路由器日志
- Linux 多台虚拟机进行同步时间
- docker 部署 笔记