本文最后更新于 213 天前,其中的信息可能已经有所发展或是发生改变。
题目大意:
N
个字符串,倒序输出。思想:
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define re register
#define fi first
#define se second
#define endl '\n'
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const int N = 1e6 + 3;
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6, PI = acos(-1);
string s[N];
void solve(){
int n; cin >> n;
for(int i = 0; i < n; i ++) cin >> s[i];
for(int i = n - 1; i >= 0; i --) cout << s[i] << endl;
}
int main(){
IOS;
int _ = 1;
// cin >> _;
while(_ --){
solve();
}
return 0;
}
题目大意:
思想:
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define re register
#define fi first
#define se second
#define endl '\n'
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const int N = 1e6 + 3;
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6, PI = acos(-1);
void solve(){
int n; cin >> n;
int cnt = 0;
for(int i = 0; i < n; i ++){
int x; cin >> x;
if(x % 2 != 0) cnt ++;
}
cout << cnt << endl;
}
int main(){
IOS;
int _ = 1;
cin >> _;
while(_ --){
solve();
}
return 0;
}
题目大意:
思想:
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define re register
#define fi first
#define se second
#define endl '\n'
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
// const int N = 1e6 + 3;
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6, PI = acos(-1);
const int N = 500;
int g[N];
int n, m;
int cnt = 0;
int find(int u){
if(g[u] != u) g[u] = find(g[u]);
return g[u];
}
void solve(){
cin >> n >> m;
for(int i = 1; i <= n; i ++) g[i] = i; //初始化
for(int i = 1; i <= m; i ++){
int a, b; cin >> a >> b;
g[find(a)] = find(b);
}
for(int i = 1; i <= n; i ++){
if(g[i] == i) cnt ++;
}
cout << cnt << endl;
}
int main(){
IOS;
int _ = 1;
// cin >> _;
while(_ --){
solve();
}
return 0;
}
题目大意:
思想:
法一:
法二:
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define re register
#define fi first
#define se second
#define endl '\n'
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const int N = 1e6 + 3;
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6, PI = acos(-1);
void solve(){
LL x; cin >> x;
for(LL i = 2; i < x ; i ++){
if(x % i == 0){
if((x / i) % i == 0) cout << i << ' ' << x / (i * i) << endl;
else cout << (LL)sqrtl(x / i) << ' ' << i << endl;
return ;
}
}
}
int main(){
IOS;
int _ = 1;
cin >> _;
while(_ --){
solve();
}
return 0;
}
题目大意:
思想:
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define re register
#define fi first
#define se second
#define endl '\n'
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const int N = 2e5 + 3;
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6, PI = acos(-1);
vector<int> g[N];
bool vis[N];
LL cnt = 0;
void dfs(int u){
if(cnt > 1e6) return ;
vis[u] = 1;
cnt ++;
for(int i = 0; i < g[u].size(); i ++){
if(vis[g[u][i]]) continue;
vis[g[u][i]] = 1;
dfs(g[u][i]);
vis[g[u][i]] = 0;
}
}
void solve(){
int n, m; cin >> n >> m;
for(int i = 0; i < m; i ++){
int a, b; cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(1);
cout << min(cnt, (LL)1000000) << endl;
}
int main(){
IOS;
int _ = 1;
// cin >> _;
while(_ --){
solve();
}
return 0;
}
题目大意:
思想:
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define re register
#define fi first
#define se second
#define endl '\n'
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
// const int N = 1e6 + 3;
// const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6, PI = acos(-1);
const ULL N = 2e6 + 9;
const int hash_cnt = 2; //哈希次数
int n;
string s;
ULL Prime[] = {1998585857ul,23333333333ul};
ULL base[] = {131, 146527, 19260817, 91815541}; // 字符集大小,进制数
ULL mod[] = {1000000007, 29123,998244353,1000000009,4294967291ull}; // 模数
ULL h1[N][hash_cnt], h2[N][hash_cnt], p[N][hash_cnt];
//初始化哈希
void initHash(ULL n, ULL cnt){
p[0][cnt] = 1;
for(int i = 1; i <= n; ++ i) p[i][cnt] = p[i - 1][cnt] * base[cnt] % mod[cnt];
for(int i = 1; i <= n; ++ i) h1[i][cnt] = (h1[i - 1][cnt] * base[cnt] % mod[cnt] + s[i]) % mod[cnt]; // 正序hash
for(int i = n; i >= 1; -- i) h2[i][cnt] = (h2[i + 1][cnt] * base[cnt] % mod[cnt] + s[i]) % mod[cnt]; // 逆序hash
}
//正序HASH
ULL getHash1(ULL id, ULL l, ULL r){
return (h1[r][id] - h1[l - 1][id] * p[r - l + 1][id] % mod[id] + mod[id]) % mod[id];
}
//逆序HASH
ULL getHash2(ULL id, ULL l, ULL r){
return (h2[l][id] - h2[r + 1][id] * p[r - l + 1][id] % mod[id] + mod[id]) % mod[id];
}
//判断区间正逆序是否相等,如果区间正逆序哈希值一样,则回文;
bool isRe(ULL id, ULL l,ULL r){
return getHash1(id, l, r) == getHash2(id, l, r);
}
void solve(){
cin >> n >> s;
s = " " + s;
initHash(2 * n, 0);
for(int i = 0; i <= n; i ++ ){
ULL sum1 = ((getHash1(0, 1, i) * p[n - i][0] % mod[0] + getHash1(0, n + i + 1, 2 * n)) % mod[0]);
ULL sum2 = getHash2(0, i + 1, n + i);
if(sum1 == sum2){
string st = s.substr(i + 1, n);
reverse(st.begin(), st.end());
cout << st << endl;
cout << i << endl;
return;
}
}
cout << -1 << endl;
}
int main(){
IOS;
int _ = 1;
// cin >> _;
while(_ --){
solve();
}
return 0;
}