#include <iostream>
#include <string>
using namespace std;
int weight(string str)
{
int len = str.size();
int count = 0;
for (int i = 0; i < len; i++)
{
if (str[i] >= 'A' && str[i] <= 'Z')
{
count++;
}
}
int newLen = len + count;
char* arr = new char[newLen+1];
int indexOld = len-1;
int indexNew = newLen-1;
while (indexOld >= 0 )
{
if (str[indexOld] >= 'A' && str[indexOld] <= 'Z')
{
arr[indexNew--] = str[indexOld] - 'A' + 'a';
arr[indexNew--] = str[indexOld] - 'A' + 'a';
}
else
{
arr[indexNew--] = str[indexOld];
}
--indexOld;
}
arr[newLen] = 0;
int res = 0;
char* p = arr;
char* q = arr+1;
int t = 1;
while (*p && *q)
{
if (*p == *q)
{
q++;
t++;
}
else
{
res += t * t * (*p - 'a' + 1);
t = 1;
p = q;
q = p+1;
}
arr = NULL;
return res;
}
int main()
{
int count;
cin >> count;
while (count--)
{
string s;
cin >> s;
cout << weight(s) << endl;
}
//getchar();
return 0;
#include <string>
using namespace std;
int weight(string str)
{
int len = str.size();
int count = 0;
for (int i = 0; i < len; i++)
{
if (str[i] >= 'A' && str[i] <= 'Z')
{
count++;
}
}
int newLen = len + count;
char* arr = new char[newLen+1];
int indexOld = len-1;
int indexNew = newLen-1;
while (indexOld >= 0 )
{
if (str[indexOld] >= 'A' && str[indexOld] <= 'Z')
{
arr[indexNew--] = str[indexOld] - 'A' + 'a';
arr[indexNew--] = str[indexOld] - 'A' + 'a';
}
else
{
arr[indexNew--] = str[indexOld];
}
--indexOld;
}
arr[newLen] = 0;
int res = 0;
char* p = arr;
char* q = arr+1;
int t = 1;
while (*p && *q)
{
if (*p == *q)
{
q++;
t++;
}
else
{
res += t * t * (*p - 'a' + 1);
t = 1;
p = q;
q = p+1;
}
}
res += t * t * (*p - 'a' + 1);
arr = NULL;
return res;
}
int main()
{
int count;
cin >> count;
while (count--)
{
string s;
cin >> s;
cout << weight(s) << endl;
}
//getchar();
return 0;
}
数据类型选错了,范围太小,做法也很愚蠢!浪费了大量时间。
做题策略也不对,应该果断放弃,看看附加题。
腾讯实习生笔试的时候也傻逼了,居然没有注意到背面不能答题,
更换答题卡,重写了一遍!最近真是各种不顺!加油,come on!
阿里面试加油!