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. Symantec NBU :Unable to retrieve version of the server xxx.xxx.xxx

    Symantec NetBackup  是赛门铁克收购的veritas公司的一款产品,该产品功能强大,据称堪称备份界的鼻祖. 其具体原理和备份方式可见:https://blog.51cto.com/s ...

  2. 安装RationalRose的问题解决

    列出大问题:在这一步无法进行下一步,直接就只能退出. 翻译过来的意思是:IBM安装程序被完全下载之前就终止了,大概是这个意思. 然后我就直接进了IBM的官网看了一下产品支持,上面解释说是组件clear ...

  3. CF1324A Yet Another Tetris Problem 题解

    原题链接 简要题意: 再简要一波: 每次可以把一个数增加 \(2\),问最后能不能让所有数相等.(也就是抵消掉) 什么?题意变成这样子还做个啥? 你会发现,必须所有数的奇偶性都相同,才可以:反之就不可 ...

  4. 快速排序-无序数组K小元素

    13:07:382020-03-10 11:16:13 问题描述: 找到一个无序数组中第K小的数 样例 1: 输入: [3, 4, 1, 2, 5], k = 3 输出: 3 样例 2: 输入: [1 ...

  5. [线段树]Codeforces 339D Xenia and Bit Operations

    Xenia and Bit Operations time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  6. 浏览器与DNS解析过程

    浏览器解析 1.地址栏输入地址后,浏览器检查自身DNS缓存 地址栏输入chrome://net-internals/#dns 查看. 2.浏览器缓存中未找到,那么Chrome会搜索操作系统自身的DNS ...

  7. Building Applications with Force.com and VisualForce (DEV401) (二四):JavaScript in Visualforce

    Dev401-025:Visualforce Pages: JavaScript in Visualforce Module Objectives1.Describe the use of AJAX ...

  8. 局部变量表中Slot复用对垃圾回收的影响详解

    看两段代码 1. package com.jvm; public class Test { public static void main(String[] args) { { byte[] plac ...

  9. Java的浅拷贝与深拷贝总结

    Java中的对象拷贝(Object Copy)指的是将一个对象的所有属性(成员变量)拷贝到另一个有着相同类类型的对象中去.举例说明:比如,对象A和对象B都属于类S,具有属性a和b.那么对对象A进行拷贝 ...

  10. MATLAB 句柄绘图

    一.线句柄实例 >> h1=line([0:10],[0:10])%创建句柄值 h1 = Line (具有属性): Color: [0 0.4470 0.7410] LineStyle: ...