我需要找到给定字符串的旋转次数,这将使其在所有旋转中按字典顺序排列最小.
例如:
原版的: ama
第一轮: maa
第二次旋转:aam这是按字典顺序排列的最小旋转,所以答案是2.
这是我的代码:
string s,tmp;
char ss[100002];
scanf("%s",ss);
s=ss;
tmp=s;
int i,len=s.size(),ans=0,t=0;
for(i=0;i<len;i++)
{
string x=s.substr(i,len-i)+s.substr(0,i);
if(x<tmp)
{
tmp=x;
t=ans;
}
ans++;
}
cout<<t<<endl;
Run Code Online (Sandbox Code Playgroud)
我正在为此解决方案获得"超出时间限制".我不明白可以进行哪些优化.如何提高解决方案的速度?
我知道struct X通过定义一个单独的散列函数struct 可以为a 定义一个散列函数:
struct hash_X {
size_t operator()(const X &x) const {}
bool operator()(const X &a, const X &b) const {}
};
int main() {
unordered_set<X, hash_X, hash_X> s;
}
Run Code Online (Sandbox Code Playgroud)
但我正在寻找operator<可以附加到struct X自身的东西,例如set:
struct X {
bool operator<(const X &other) const {}
};
int main() {
set<X> s;
}
Run Code Online (Sandbox Code Playgroud)
最终目标是这样的:
struct X {
size_t operator()(void) const {}
bool operator()(const X &other) const {}
};
int main() {
unordered_set<X> s;
}
Run Code Online (Sandbox Code Playgroud)
这在C++中是否可行?
为什么assertFalse成功None?
import unittest
class TestNoneIsFalse(unittest.TestCase):
def test_none_is_false(self):
self.assertFalse(None)
Run Code Online (Sandbox Code Playgroud)
结果:
> python -m unittest temp
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
Run Code Online (Sandbox Code Playgroud)
似乎这种行为会引发错误,而函数并不总是返回值.例如:
def is_lower_than_5(x):
if x < 5:
return True
elif x > 5:
return False
....
def test_5_is_not_lower_than_5(self):
self.assertFalse(is_lower_than_5(5))
Run Code Online (Sandbox Code Playgroud)
即使它失败,上述测试也会通过.它缺少应该捕获的代码中的错误.
我们应该如何断言值在字面上False而不仅仅是布尔上下文中的错误?例如
self.assertEquals(False, None) # assert fails. good!
Run Code Online (Sandbox Code Playgroud) 我有一个可执行文件example.exe。此可执行文件的行为如下:
1.Waits for input from user
2.Performs some operations, based on input
3.goto 1
Run Code Online (Sandbox Code Playgroud)
我如何使用subprocess或类似的模块与可执行文件进行交互?
我希望运行该过程,插入输入,接收输出,然后根据接收到的输出插入其他输入。
以下程序使用构造列表中的URL itertools.permutations。
def url_construct_function():
for i in range(1, len(samplelist)):
for item in list(permutations(samplelist, i)):
Run Code Online (Sandbox Code Playgroud)
a,b,citertools.permutations 围绕各种可能的有序组合提供了很好的描述
我想让程序理解这一点,a,b并且b,a是相同的。
代码片段:
def copy_additional_files(self):
try:
for filetocopy in self.files_to_copy:
shutil.copy2(FileToCopy.get_source, FileToCopy.get_target)
except NotImplementedError:
logging.error('keep thinking')
raise EwaException
Run Code Online (Sandbox Code Playgroud)
get_source是一个@property返回字符串的 -shutil.copy2需要一个字符串才能工作
我的 IDE (PyCharm 4.0.6) 告诉我它需要一个字符串,但得到了一个属性。
这是一个错误吗?一个属性可以返回一个字符串,因此additional_file_source也可以是一个字符串(正如它预期的那样),但是当我打印它时它返回属性对象的对象 ID:
<property object at 0x....> <property object at 0x....>
Run Code Online (Sandbox Code Playgroud)
并抛出:
“属性”不支持缓冲区接口
有没有更简单和明显的方法来做到这一点而不会出错?
这个循环有什么问题?
int index = 0;
for(int x = 0; x < winDate.length;x++);
{
if(userDate == winDate[x])
{
index = x;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
我在比较数值之前曾多次使用过x.
python ×4
c++ ×2
algorithm ×1
arrays ×1
dynamic ×1
java ×1
loops ×1
properties ×1
pycharm ×1
python-2.7 ×1
string ×1
typing ×1
unit-testing ×1