QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#625310#9430. Left Shifting 2Dung1604AC ✓6ms7700kbC++173.7kb2024-10-09 18:35:092024-10-09 18:35:11

Judging History

你现在查看的是最新测评结果

  • [2024-10-09 18:35:11]
  • 评测
  • 测评结果:AC
  • 用时:6ms
  • 内存:7700kb
  • [2024-10-09 18:35:09]
  • 提交

answer

#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <tuple>
#include <iomanip>
#include <map>
#include <algorithm>
#include <math.h>
#include <string>
#include <vector>
#include <unordered_map>
#define ll long long
#define inf 10000000000007
#define mod 1000000007

using namespace std;

const int BLOCK = 450;


ll fastpow(ll n, ll x) {

	if (x == 0) {
		return 1;
	}
	else {
		ll ret = fastpow(n, x / 2);
		ret = ((ret % mod) * (ret % mod)) % mod;
		if (x % 2 == 0) {
			return ret;
		}
		else {
			return ((ret) * (n)) % mod;
		}
	}
}


ll gcd(ll a, ll b) {
	if (a == 0) {
		return b;
	}
	if (b == 0) {
		return a;
	}
	else {
		return gcd(b, a % b);
	}
}
ll lcm(ll a, ll b) {
	ll val = (a % mod * b % mod) % mod;
	val = (val * fastpow(gcd(a, b), mod - 2)) % mod;
	return val;
}
int Logk(ll n, ll k) {
	if (k == 1) {
		return 1;
	}
	if (n == 0) {
		return 0;
	}
	int count = -1;
	while (n > 0) {
		count++;
		n /= k;
	}
	return count;
}
struct Dsu {
	vector<int> par;

	void init(int n) {
		par.resize(n + 5, 0);
		for (int i = 1; i <= n; i++) par[i] = i;
	}

	int find(int u) {
		if (par[u] == u) return u;
		return par[u] = find(par[u]);
	}

	bool join(int u, int v) {
		u = find(u); v = find(v);
		if (u == v) return false;
		par[v] = u;
		return true;
	}
} dsu;


void solve() {
	string s;
	cin >> s;
	int n = s.length();
	vector<int> lap_end(n);
	vector<int> lap_start(n);
	ll cur = 0;
	for (int i = 0; i < n; i++) {
		int pos = -1;
		for (int j = i; j < n; j++) {
			if (s[j] == s[i]) {
				pos = j;
			}
			else {
				break;
			}
		}
		for (int j = i; j <= pos; j++) {
			lap_end[j] = pos;
			lap_start[j] = i;
		
		}
		cur += (pos - i + 1) / 2;
		i = pos;
		

	}

	ll ans = cur;
	
	for (int d = 1; d < n; d++) {
		ll ret = cur;
		int part1 = d - 1;
		
		if (lap_end[part1]  > part1) {
			
			ret -= (lap_end[part1] - lap_start[part1] + 1) / 2;
			if (lap_end[part1] == n - 1) {
				if (s[n - 1] != s[0]) {
					ret += (lap_end[part1] - part1) / 2;
					ret += (part1 - lap_start[part1] + 1) / 2;
				}
				else {
					if (lap_start[part1] == 0) {
						
						ret += (lap_end[part1] - lap_start[part1] + 1) / 2;
					}
					else {
						ret -= (lap_end[0] - lap_start[0] + 1) / 2;
						ret += (part1 - lap_start[part1] + 1) / 2;

						ret += (lap_end[part1] - part1 + lap_end[0] - lap_start[0] + 1) / 2;
					}
				}
			}
			else {
				

				ret += (lap_end[part1] - part1) / 2;
				if (lap_start[part1] == 0) {
					
					if (s[n - 1] == s[0]) {
						ret -= (lap_end[n-1] - lap_start[n-1] + 1) / 2;
						ret += (part1 - lap_start[part1] + 1 + lap_end[n-1] - lap_start[n-1] + 1) / 2;
					}
					else {
						ret += (part1 - lap_start[part1] + 1) / 2;
					}
					

				}
				else {
					ret += (part1 - lap_start[part1] + 1) / 2;
					if (s[n - 1] == s[0]) {
						ret -= (lap_end[n - 1] - lap_start[n - 1] + 1) / 2;
						ret -= (lap_end[0] - lap_start[0] + 1) / 2;
						
						ret += (lap_end[0] - lap_start[0] + 1 + lap_end[n - 1] - lap_start[n - 1] + 1) / 2;
					}
				}
				
			}
			
			
			
			
			
		}
		else {
			if (s[n - 1] == s[0]) {
				ret -= (lap_end[n - 1] - lap_start[n - 1] + 1) / 2;
				ret -= (lap_end[0] - lap_start[0] + 1) / 2;
				ret += (lap_end[0] - lap_start[0] + 1 + lap_end[n - 1] - lap_start[n - 1] + 1)/2;
			}
		}
		
		
		
		ans = min(ans, ret);
	}
	cout << ans << endl;
	
}



int main() {




	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
	int t;
	cin >> t;
	while (t--) {
		solve();
	}






























}

这程序好像有点Bug,我给组数据试试?

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3584kb

input:

3
abccbbbbd
abcde
x

output:

2
0
0

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 3ms
memory: 3788kb

input:

5000
lfpbavjsmppdppkfwnyfmbdhptdswsoulrbhyjh
cfliuqnynejgnxolzbdoztclzbozqomvioszk
eiivcoqoymonrqgrjdtkts
mdcvservaxcbioopfungsgkiftchwlmtbzqgaraovjknsgiegkvdbolmeunvrxrpscnf
ujeqtidwtoikkqtygo
llma
qjfvgwrdhaazejsfgilnpmmhkefndzvyon
kzwwpdpbrudqmwmjscllnnjyoepxophcoopvfepikouinuxx
vftculoorxskpkxoz...

output:

1
0
0
0
0
0
1
4
0
0
1
1
1
1
1
3
1
0
5
6
0
0
5
2
0
1
3
2
0
3
0
1
0
1
1
0
1
4
1
3
1
0
1
5
3
0
3
0
0
1
8
1
0
6
1
2
0
1
0
0
4
1
2
4
3
1
3
2
3
1
2
1
0
0
2
0
2
2
0
4
0
5
5
0
3
0
4
1
0
2
1
0
2
0
1
6
1
2
1
3
3
3
5
2
3
0
3
5
1
3
0
0
3
0
4
5
3
2
1
1
0
0
2
0
1
1
3
3
3
1
2
0
1
1
4
3
1
3
1
1
1
2
0
1
2
0
4
0
1
1
...

result:

ok 5000 lines

Test #3:

score: 0
Accepted
time: 4ms
memory: 7632kb

input:

1
cbppzfsncqyzmuwrcvtxsciucxusskcjhaanwhqmyncytwhkubrvcqxgcehdxyewdyvpqjcmrnmlgrytrucexmmfulqbtfctehphmrzkosyvhtvjrromqncbgsjcwhmlqidkycaxyhsrduoxayntuhqubvboseeziwjvrfagsbvtxjjbexnajqapgxydwtztzbbdpoydnjipfizdfpmczgqvdmpvxbqubtygkfpdeonegfzsttirbhzkobbigwneyvtcxndfkljdvbbcfnadtfhgohfzqeidtgyandhnvb...

output:

18631

result:

ok single line: '18631'

Test #4:

score: 0
Accepted
time: 2ms
memory: 7652kb

input:

1
qokalgqjhyijyizyihdsiapbgvzxzevykavqmgqzrpjngciqcljsuplvpaziebmumatzvngwrhgsxrtcoiseihejwpewvosnrgvhoxluliuwixgxylazufebrwgfebazrkghgwbpqavehtnakmzqsetghmzoydwmeqvoplkyqngwrgktylrnaojpkvuwfsjbizedqwhfteyjobrglkhkeoxmxdgyuygawvdjhyakpkjchyxaqthrglcldevrzskdaotkbsbmnstrsxervdvmaylqxnwaecfmdszwedrdom...

output:

0

result:

ok single line: '0'

Test #5:

score: 0
Accepted
time: 2ms
memory: 7608kb

input:

1
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbyyqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccceeeeeeeeeeeeeeeeeeeeezzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz...

output:

246800

result:

ok single line: '246800'

Test #6:

score: 0
Accepted
time: 6ms
memory: 7700kb

input:

1
yyyyyyyyyyyyyyyyyyyyyyyyhhhhhhhhaaaannnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnhhhhhhhhhhhhhhhhhhhhhhhiiiiiiiieeeeeeeeeesssssssbbbbbbbbbbiiiiiiiwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxaaaaaaaaaaaaaaaadccccckkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkc...

output:

242729

result:

ok single line: '242729'

Extra Test:

score: 0
Extra Test Passed