C    HDU_5578

求字符串中所有相同字母的最小距离H。

Input

实例个数T
然后T行字符串
字符串中仅含有小写字母。
1≤T≤50
1≤len≤1000(len为字符串长度)

Output

对于每个实例输出Case #t: H,表示第t个实例的答案是H。 如果没有相同的字母,H=-1。

Sample Input

2

ac

wxzayzxw

Sample Output

Case #1: -1

Case #2: 3

纯模拟_两层循环

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
char str[];
int main() {
int t;
scanf("%d",&t);
int cnt=;
while(t--) {
scanf("%s",str);
int len=strlen(str);
int ans=,flag=;
for(int i=; i<len; i++) {
for(int j=i+; j<len; j++) {
if(str[j]==str[i]) {
ans=min(ans,j-i);
flag=;
break;
}
}
}
if(!flag)ans=-;
printf("Case #%d: %d\n",cnt++,ans);
}
return ;
}

另一种更快一点的_一层循环_利用数组存前导位置

 #include<cstdio>
#include<cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
char s[]; int x[];
int l;
int g=;
int main() {
int T;
cin>>T;
int k=;
while(T--) {
memset(x,-,sizeof(x));
memset(s,,sizeof(s));
l=;
//g=0;
scanf("%s",s);
int len=strlen(s);
for(int i=; i<len; i++) {
int t=s[i]-'a';
if(x[t]!=-) {
int l1=i-x[t];
l=min(l,l1);
}
x[t]=i;
}
if(l==)l=-;
cout<<"Case #"<<++k<<": "<<l<<endl;
}
return ;
}

F    HDU_5583

现在有一个仅含0和1的字符串,规定字符串值的计算法方法是:
将字符串分成很多子串(连续),每个子串中不能同时含0和1。字符串的值为每个子串长度的平方和。例如00110 值为2*2+2*2+1*1=9。 
这对你来说太简单了,你所要做的是更改字符串中的最多一个字符(可不改),当然了只能改为0或1,求出更改之后的字符串的最大值。

Input

实例个数T 
然后T行字符串 
1≤T≤50 
1≤len≤100000 (len为字符串长度)

Output

对于每个实例输出

Case #t: ans,表示第t个实例的答案是ans。

Sample Input

2
00110
0010

Sample Output

Case #1: 13
Case #2: 16

纯模拟_TE了的答案_即使模拟也要注意最后一个长度记录下来

 #include<cstdio>
#include<cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
char s[]; int x[];
ll l;
int g=;
int len;
ll f() { char g=;
ll lx=;
ll lt=;
for(int i=; i<len; i++) {
if(g==s[i]) {
lx++;
} else {
lt+=lx*lx;
//cout<<g<<": "<<lx<<endl;
g=s[i];
lx=;
}
if(i==len-) {
lt+=lx*lx;
//cout<<g<<": "<<lx<<endl;
}
}
return lt;
}
int main() {
int T;
cin>>T;
int k=;
while(T--) {
memset(s,,sizeof(s));
//g=0;
scanf("%s",s);
len=strlen(s);
l=f();
for(int i=; i<len; i++) {
if(s[i]=='') {
s[i]='';
///cout<<f()<<endl;
l=max(l,f());
s[i]='';
} else {
s[i]='';
//cout<<f()<<endl;
l=max(l,f());
s[i]='';
}
}
printf("Case #%d: %lld\n",++k,l); }
return ;
}

改了一下,  贪心, 想要平方和最大, 已知, a^2+b^2<=(a+b)^2,显然不希望砍断长串,所以在各个串的边界改,

只能改一个, 那实际上就是求边界改的结果,每次改, 影响的是一对相邻串

如果模拟还是每次都要算一次(循环一次), 很有可能再TE, 那就转化成计算, 改边界,就是,某相邻串长度一个-1,一个+1,算此时新的结果,

和模拟一样也要注意最后一个记录下来

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;
const int N=1e5+;
ll dp[N];
char str[N];
int len;
int num;
ll sum;
ll ans;
void f() {
dp[]=;
for(int i=; i<len; i++) {
if(str[i]==str[i-]) {
dp[num]++;
} else {
sum+=dp[num]*dp[num];
num++;
dp[num]++;
}
}
sum+=dp[num]*dp[num];
if(num==) {
ans=dp[]*dp[];
}
}
int main() {
int t;
scanf("%d",&t);
int k=;
while(t--) {
num=;
ans=;
sum=;
scanf("%s",str);
len=strlen(str);
memset(dp,,sizeof(dp));
f();
for(int i=; i<=num; i++) {
if(dp[i]==) {
ans=max(ans,sum+*(dp[i-]*dp[i+]+dp[i-]+dp[i+]));
} else {
if(dp[i-]>=dp[i]) {
ans=max(ans,sum+*(dp[i-]-dp[i]+));
} else ans=max(ans,sum+*(dp[i]-dp[i-]+));
}
}
cout<<"Case #"<<++k<<": "<<ans<<"\n";
}
return ;
}
 

week7_简单题_C_水题_hdu_5578+F_贪心_hdu_5583的更多相关文章

  1. ytu 1304:串的简单处理(水题)

    串的简单处理 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 39  Solved: 11[Submit][Status][Web Board] Desc ...

  2. 每日一刷(2018多校水题+2016icpc水题)

    11.9 线段树 http://acm.hdu.edu.cn/showproblem.php?pid=6315 求逆序对个数 http://acm.hdu.edu.cn/showproblem.php ...

  3. sdut 2413:n a^o7 !(第三届山东省省赛原题,水题,字符串处理)

    n a^o7 ! Time Limit: 1000MS Memory limit: 65536K 题目描述 All brave and intelligent fighters, next you w ...

  4. sdut 2163:Identifiers(第二届山东省省赛原题,水题)

    Identifiers Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述  Identifier is an important c ...

  5. HDU 4772 Zhuge Liang's Password (2013杭州1003题,水题)

    Zhuge Liang's Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  6. sdut 2154:Shopping(第一届山东省省赛原题,水题)

    Shopping Time Limit: 1000MS Memory limit: 65536K 题目描述 Saya and Kudo go shopping together.You can ass ...

  7. HDU 5776 sum (BestCoder Round #85 A) 简单前缀判断+水题

    分析:就是判断简单的前缀有没有相同,注意下自身是m的倍数,以及vis[0]=true; #include <cstdio> #include <cstdlib> #includ ...

  8. sdut 2158:Hello World!(第一届山东省省赛原题,水题,穷举)

    Hello World! Time Limit: 1000MS Memory limit: 65536K 题目描述 We know that Ivan gives Saya three problem ...

  9. CodeForces-2015 HIAST Collegiate Programming Contest-Gym-100952A.水题 100952B.水题 100952C.回文字符串 100952D.杨辉三角处理组合数 其他题目待续。。。

    哈哈哈哈哈哈哈,最近一直在补题,改各种错误的代码,wa了20多遍,改到心态爆炸,改好之后,感觉世界都美好了(叉会腰~)... A. Who is the winner? time limit per ...

随机推荐

  1. 分享CCNTFS小工具,在 macOS 中完全读写、修改、访问Windows NTFS硬盘的文件,无须额外的驱动(原生驱动)更稳定,简单设置即可高速传输外接NTFS硬盘文件

    CCNTFS [ 下载 ] 在 macOS 中完全读写.修改.访问Windows NTFS硬盘的文件,无须额外的驱动(原生驱动)更稳定,安装后进行简单设置即可高速传输外接NTFS硬盘文件,可全程离线使 ...

  2. macOS开发:调整NSImage尺寸大小

    原文链接 extension NSImage { func resize(_ to: CGSize, isPixels: Bool = false) -> NSImage { var toSiz ...

  3. Ansible权威指南-读书笔记

    2 Ansible基础元素介绍 2.1 ansible 目录结构介绍 2.2 ansible 配置文件解析 配置文件解析顺序:当前命令执行目录-->用户家目录下的.ansible.cfg--&g ...

  4. 搭建生产级的Netty项目

    Netty是Trustin Lee在2004年开发的一款高性能的网络应用程序框架.相比于JDK自带的NIO,Netty做了相当多的增强,且隔离了jdk nio的实现细节,API也比较友好,还支持流量整 ...

  5. Building Applications with Force.com and VisualForce (DEV401) (二五):Visualforce Controller

    Dev401-026:Visualforce Pages: Visualforce Controller   Module Objectives1.Identify the functionality ...

  6. 使用室内三维地图引擎ESMap来管理摄像头设备、消防设备和人员轨迹展示

    目前室内三维地图如何轻量化,能够在手机微信.电脑浏览器等平台快速显示地图,显示的地图性能好,转动地图不卡是大家都要面对的问题, 使用室内三维地图引擎ESMap后目前可以不用操心这方面的问题,开发只需要 ...

  7. js Object方法小结

    1. Object.defineProperty(obj,prop,{                 value:...,                 writable:boolean,//可写 ...

  8. Mob之社会化分享集成ShareSDK

    接着上篇顺便分享一篇自己使用 ShareSDK 的笔记,上篇我们集成了 SMSSDK 完成了短信接收验证码的功能,请参考Mob 之 短信验证集成 SMSSDK,如何在项目已经集成 SMSSDK 的情况 ...

  9. A - Investment

    A - Investment John never knew he had a grand-uncle, until he received the notary's letter. He learn ...

  10. 【Net】ABP框架学习之它并不那么好用

    前言 上一篇文章介绍了ABP的Web API,本文在继续介绍ABP的其他内容. 在ABP中,WEBAPI是一个值得用的东西.但其他东西,就不一定是那么好用了. 下面我们看一下ABP的Controlle ...