随机字符串
import random, string
random_str = ''.join(random.choice(string.ascii_lowercase) for _ in range(800000))
print(random_str, file= open("5.in", "w")) string库/usr/lib/python3.8/string.py
"""
whitespace -- a string containing all ASCII whitespace
ascii_lowercase -- a string containing all ASCII lowercase letters
ascii_uppercase -- a string containing all ASCII uppercase letters
ascii_letters -- a string containing all ASCII letters
digits -- a string containing all ASCII decimal digits
hexdigits -- a string containing all ASCII hexadecimal digits
octdigits -- a string containing all ASCII octal digits
punctuation -- a string containing all ASCII punctuation characters
printable -- a string containing all ASCII characters considered printable
"""
whitespace = ' \t\n\r\v\f'
ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_letters = ascii_lowercase + ascii_uppercase
digits = '0123456789'
hexdigits = digits + 'abcdef' + 'ABCDEF'
octdigits = '01234567'
punctuation = r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
printable = digits + ascii_letters + punctuation + whitespace
测试时间 重新导向控制台
#include <stdio.h>
#include <string.h>
#include <time.h>
#define N 1000005
char s[N], t[N];
int main() {
freopen("5.in", "r", stdin);
freopen("5.out", "w", stdout);
time_t begin_t = clock();
while (~scanf("%s", s)) {
int n = strlen(s), cnt = 0, j = 0;
for (int i = 0; i < n; ++i)
if (s[i] == 'x') ++cnt;
else t[j++] = s[i];
for (int i = 0; i < cnt; ++i) t[j++] = 'x';
t[j] = 0;
puts(t);
}
freopen("CON", "w", stdout);
time_t finish_t = clock();
printf("it cost %lf s\n", (double)(finish_t - begin_t) / CLOCKS_PER_SEC);
return 0;
}
需要注意,这里其实没有真正关闭,只是再次重定向,回到控制台。
在windows/DOS,读文件后用freopen("CON", "r", stdin),写文件后 freopen("CON","w", stdout)。
在linux中,控制台设备是 /dev/console:freopen("/dev/console", "r", stdin)。

京公网安备 11010502036488号