uva10716 - Evil Straw Warts Live(转换成回文数)
来源:互联网 发布:网络消费者权益保护 编辑:程序博客网 时间:2024/06/01 08:10
如果有人说这道题是大水题,我无话可说
但是我不得不说的是这道题卡到我崩溃了。。。
思路正确就是WA,
所以我就一直改,一直交,直到AC
说说我犯错的地方,
1、对于奇数次出现的字符处理不正确
2、对于字符串移动,出现了差错
要想A掉这道题,必须要想办法过了下面这道样例
input:aaabbbb
output:6
多点思考,多点认真,,,(只能对自己这么说了!!!)
代码如下:
#include <cstdio>#include <cstring>#include <cstdlib>int main (){ char s[150]; int len, cas, ans, angle, f[30]; scanf("%d",&cas); getchar(); while(cas--) { scanf("%s",s); getchar(); len = strlen(s); memset(f,0,sizeof(f)); for(int i = 0; i < len; i++) f[s[i]-'a']++; //奇数次出现的字符最多有一个 ans = 0; for(int i = 0; i < 26; i++) ans+=f[i]%2; if(ans>1) { puts("Impossible"); continue; } int rear = 0, front = len-1, lr, lf; ans = 0; if(len%2) { int tt, cnt = 0; for(int i = 0; i < len; i++) if(f[s[i]-'a']%2) { tt = f[s[i]-'a']; if(cnt++==tt/2) {s[i] = '*'; break;}}//改变奇数次出现的字符中最中间的字符的值 } while(rear<=front) { if(s[rear]=='*') {angle = rear; rear++; continue;} else if(s[front]=='*') {angle = front; front--; continue;}//如果是特殊字符就跳过。 for(int i = front; i > rear; i--) if(s[i]==s[rear]) {lr = front-i; break;} for(int i = rear; i < front; i++) if(s[i]==s[front]){lf = i-rear; break; }//从两个方向找最小移动次数 if(lr<=lf) { ans+=lr; for(int i = front-lr; i < front; i++) s[i] = s[i+1]; s[front] = s[rear]; } else if(lf<lr) { ans+=lf; for(int i = rear+lf; i > rear; i--) s[i] = s[i-1]; s[rear] = s[front]; } rear++; front--; } if(len%2==0) printf("%d\n",ans); else printf("%d\n",ans+abs(len/2-angle)); } return 0;}
- uva10716 - Evil Straw Warts Live(转换成回文数)
- Evil Straw Warts Live (Uva10716 回文串+贪心)
- UVA10716 - Evil Straw Warts Live
- uva10716 - - Evil Straw Warts Live
- [贪心]UVA10716 - Evil Straw Warts Live
- UVA 10716 Evil Straw Warts Live 回文数 贪心
- Evil Straw Warts Live
- POJ Evil Straw Warts Live (贪心)
- 10716 - Evil Straw Warts Live
- UVaOJ10716 - Evil Straw Warts Live
- Evil Straw Warts Live uva+回文串+贪心
- uva--10716Evil Straw Warts Live +回文串+贪心
- uva 10716 Evil Straw Warts Live(贪心回文串)
- UVA_10716 - Evil Straw Warts Live-------水题(模拟)
- UVA 10716 Evil Straw Warts Live(贪心)
- uva 10716 Evil Straw Warts Live(贪心)
- uva - 10716 - Evil Straw Warts Live(贪心、模拟)
- UVA-10716 Evil Straw Warts Live(贪心+模拟)
- Java JDK版本说明
- JDBC以及相关技术学习(一)----第一个JDBC程序
- linux系统下proc文件的含义
- 一步一步教你搭建win7下的Node.js服务(来玩玩服务器端的javascript吧,这可不是前端js插件)
- 基于指定数组大小的切割字符串(java字符串分割性能比较)
- uva10716 - Evil Straw Warts Live(转换成回文数)
- 《硅谷禁书》 pdf
- MMU的通俗理解
- android中listview卡顿的终极原因解析
- Linux 文件系统
- Node.js:用JavaScript写服务器端程序-介绍并写个MVC框架
- AS3搭建开发环境
- 字符串“This is a test”将其字符串中单词首字符大写后输出(“This Is A Test”)。
- ios调用webservice时,获取返回的结果